public function lintPath($path) { $data = $this->getData($path); try { $parser = new PhutilJSONParser(); $parser->parse($data); } catch (PhutilJSONParserException $ex) { $this->raiseLintAtLine($ex->getSourceLine(), $ex->getSourceChar(), $this->getLinterName(), $ex->getMessage()); } }
public function testDuplicateKeys() { $parser = new PhutilJSONParser(); $tests = array('{"foo": "bar", "foo": "baz"}' => array('foo' => 'baz')); foreach ($tests as $input => $expect) { $parser->setAllowDuplicateKeys(true); $this->assertEqual($expect, $parser->parse($input), 'Parsing JSON: ' . $input); $parser->setAllowDuplicateKeys(false); $caught = null; try { $parser->parse($input); } catch (Exception $ex) { $caught = $ex; } $this->assertTrue($caught instanceof PhutilJSONParserException); } }
/** * Decode a JSON dictionary. * * @param string A string which ostensibly contains a JSON-encoded list or * dictionary. * @return mixed Decoded list/dictionary. */ function phutil_json_decode($string) { $result = @json_decode($string, true); if (!is_array($result)) { // Failed to decode the JSON. Try to use @{class:PhutilJSONParser} instead. // This will probably fail, but will throw a useful exception. $parser = new PhutilJSONParser(); $result = $parser->parse($string); } return $result; }