3 final class PhabricatorJSONDocumentEngine
4 extends PhabricatorTextDocumentEngine
{
6 const ENGINEKEY
= 'json';
8 public function getViewAsLabel(PhabricatorDocumentRef
$ref) {
9 return pht('View as JSON');
12 protected function getDocumentIconIcon(PhabricatorDocumentRef
$ref) {
16 protected function getContentScore(PhabricatorDocumentRef
$ref) {
18 $name = $ref->getName();
20 if (preg_match('/\.json\z/', $name)) {
25 if ($ref->isProbablyJSON()) {
32 protected function newDocumentContent(PhabricatorDocumentRef
$ref) {
33 $raw_data = $this->loadTextData($ref);
36 $data = phutil_json_decode($raw_data);
38 // See T13635. "phutil_json_decode()" always turns JSON into a PHP array,
39 // and we lose the distinction between "{}" and "[]". This distinction is
40 // important when rendering a document.
41 $data = json_decode($raw_data, false);
43 throw new PhabricatorDocumentEngineParserException(
45 'Failed to "json_decode(...)" JSON document after successfully '.
46 'decoding it with "phutil_json_decode(...).'));
49 if (preg_match('/^\s*\[/', $raw_data)) {
50 $content = id(new PhutilJSON())->encodeAsList($data);
52 $content = id(new PhutilJSON())->encodeFormatted($data);
56 $content = PhabricatorSyntaxHighlighter
::highlightWithLanguage(
59 } catch (PhutilJSONParserException
$ex) {
60 $message = $this->newMessage(
62 'This document is not valid JSON: %s',
66 } catch (PhabricatorDocumentEngineParserException
$ex) {
67 $message = $this->newMessage(
69 'Unable to parse this document as JSON: %s',
77 $this->newTextDocumentContent($ref, $content),