1
0
Fork 0
mirror of https://we.phorge.it/source/arcanist.git synced 2024-11-29 02:02:40 +01:00
phorge-arcanist/src/parser/PhutilJSONParser.php
epriestley acf0607683 Merge utility/support changes from "wilds" to "master"
Summary:
Ref T13395. Merge a lot of stuff which doesn't break existing workflows:

    - Merge a UTF8 fix for "cmd.exe" on Windows.
    - Merge minor changes to JSON linters.
    - Merge some shell completion stuff.
    - Merge some "arc anoid" fixes.
    - Merge various Windows improvements to unit tests which interact with processes / the filesystem.
    - Merge some other Windows path fixes.
    - Merge a UTF8 character class fix.
    - Merge script initialization.
    - Merge unit test support scripts.
    - Merge some initialization code.
    - Merge Windows stdout/stderr-as-files code.
    - Merge a bunch of code for making exec tests work on Windows.
    - Merge more Windows unit test fixes.
    - Merge "continue on failure" mode when loading symbols.
    - Merge "GPC" order CLI fixes.

Test Plan: Ran `arc unit --everything`; created this change. There's likely some less-than-perfect code here.

Maniphest Tasks: T13395

Differential Revision: https://secure.phabricator.com/D20988
2020-02-13 14:10:09 -08:00

64 lines
1.8 KiB
PHP

<?php
/**
* A JSON parser.
*
* @phutil-external-symbol class JsonLintJsonParser
* @phutil-external-symbol class JsonLintParsingException
*/
final class PhutilJSONParser extends Phobject {
private $allowDuplicateKeys = false;
public function setAllowDuplicateKeys($allow_duplicate_keys) {
$this->allowDuplicateKeys = $allow_duplicate_keys;
return $this;
}
public function parse($json) {
$arcanist_root = phutil_get_library_root('arcanist');
$jsonlint_root = $arcanist_root.'/../externals/jsonlint';
require_once $jsonlint_root.'/src/Seld/JsonLint/JsonParser.php';
require_once $jsonlint_root.'/src/Seld/JsonLint/Lexer.php';
require_once $jsonlint_root.'/src/Seld/JsonLint/ParsingException.php';
require_once $jsonlint_root.'/src/Seld/JsonLint/Undefined.php';
$parser = new JsonLintJsonParser();
try {
$output = $parser->parse($json, $this->getFlags());
} catch (JsonLintParsingException $ex) {
$details = $ex->getDetails();
$message = preg_replace("/^Parse error .*\\^\n/s", '', $ex->getMessage());
throw new PhutilJSONParserException(
$message,
idx(idx($details, 'loc', array()), 'last_line'),
idx(idx($details, 'loc', array()), 'last_column'),
idx($details, 'token'),
idx($details, 'expected'));
}
if (!is_array($output)) {
throw new PhutilJSONParserException(
pht(
'%s is not a valid JSON object.',
PhutilReadableSerializer::printShort($json)));
}
return $output;
}
private function getFlags() {
$flags = JsonLintJsonParser::PARSE_TO_ASSOC;
if ($this->allowDuplicateKeys) {
$flags |= JsonLintJsonParser::ALLOW_DUPLICATE_KEYS;
} else {
$flags |= JsonLintJsonParser::DETECT_KEY_CONFLICTS;
}
return $flags;
}
}