Changeset View
Changeset View
Standalone View
Standalone View
src/applications/files/document/PhabricatorJSONDocumentEngine.php
Show All 25 Lines | final class PhabricatorJSONDocumentEngine | ||||
} | } | ||||
protected function newDocumentContent(PhabricatorDocumentRef $ref) { | protected function newDocumentContent(PhabricatorDocumentRef $ref) { | ||||
$raw_data = $this->loadTextData($ref); | $raw_data = $this->loadTextData($ref); | ||||
try { | try { | ||||
$data = phutil_json_decode($raw_data); | $data = phutil_json_decode($raw_data); | ||||
// See T13635. "phutil_json_decode()" always turns JSON into a PHP array, | |||||
// and we lose the distinction between "{}" and "[]". This distinction is | |||||
// important when rendering a document. | |||||
$data = json_decode($raw_data, false); | |||||
if (!$data) { | |||||
throw new PhabricatorDocumentEngineParserException( | |||||
pht( | |||||
'Failed to "json_decode(...)" JSON document after successfully '. | |||||
'decoding it with "phutil_json_decode(...).')); | |||||
} | |||||
if (preg_match('/^\s*\[/', $raw_data)) { | if (preg_match('/^\s*\[/', $raw_data)) { | ||||
$content = id(new PhutilJSON())->encodeAsList($data); | $content = id(new PhutilJSON())->encodeAsList($data); | ||||
} else { | } else { | ||||
$content = id(new PhutilJSON())->encodeFormatted($data); | $content = id(new PhutilJSON())->encodeFormatted($data); | ||||
} | } | ||||
$message = null; | $message = null; | ||||
$content = PhabricatorSyntaxHighlighter::highlightWithLanguage( | $content = PhabricatorSyntaxHighlighter::highlightWithLanguage( | ||||
'json', | 'json', | ||||
$content); | $content); | ||||
} catch (PhutilJSONParserException $ex) { | } catch (PhutilJSONParserException $ex) { | ||||
$message = $this->newMessage( | $message = $this->newMessage( | ||||
pht( | pht( | ||||
'This document is not valid JSON: %s', | 'This document is not valid JSON: %s', | ||||
$ex->getMessage())); | $ex->getMessage())); | ||||
$content = $raw_data; | $content = $raw_data; | ||||
} catch (PhabricatorDocumentEngineParserException $ex) { | |||||
$message = $this->newMessage( | |||||
pht( | |||||
'Unable to parse this document as JSON: %s', | |||||
$ex->getMessage())); | |||||
$content = $raw_data; | |||||
} | } | ||||
return array( | return array( | ||||
$message, | $message, | ||||
$this->newTextDocumentContent($ref, $content), | $this->newTextDocumentContent($ref, $content), | ||||
); | ); | ||||
} | } | ||||
} | } |