diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -3187,6 +3187,7 @@ 'PhabricatorIteratorFileUploadSource' => 'applications/files/uploadsource/PhabricatorIteratorFileUploadSource.php', 'PhabricatorJIRAAuthProvider' => 'applications/auth/provider/PhabricatorJIRAAuthProvider.php', 'PhabricatorJSONConfigType' => 'applications/config/type/PhabricatorJSONConfigType.php', + 'PhabricatorJSONDocumentEngine' => 'applications/files/document/PhabricatorJSONDocumentEngine.php', 'PhabricatorJSONExportFormat' => 'infrastructure/export/format/PhabricatorJSONExportFormat.php', 'PhabricatorJavelinLinter' => 'infrastructure/lint/linter/PhabricatorJavelinLinter.php', 'PhabricatorJiraIssueHasObjectEdgeType' => 'applications/doorkeeper/edge/PhabricatorJiraIssueHasObjectEdgeType.php', @@ -8800,6 +8801,7 @@ 'PhabricatorIteratorFileUploadSource' => 'PhabricatorFileUploadSource', 'PhabricatorJIRAAuthProvider' => 'PhabricatorOAuth1AuthProvider', 'PhabricatorJSONConfigType' => 'PhabricatorTextConfigType', + 'PhabricatorJSONDocumentEngine' => 'PhabricatorTextDocumentEngine', 'PhabricatorJSONExportFormat' => 'PhabricatorExportFormat', 'PhabricatorJavelinLinter' => 'ArcanistLinter', 'PhabricatorJiraIssueHasObjectEdgeType' => 'PhabricatorEdgeType', diff --git a/src/applications/files/document/PhabricatorDocumentRef.php b/src/applications/files/document/PhabricatorDocumentRef.php --- a/src/applications/files/document/PhabricatorDocumentRef.php +++ b/src/applications/files/document/PhabricatorDocumentRef.php @@ -116,6 +116,10 @@ } $snippet = $this->getSnippet(); + if (!preg_match('/^\s*[{[]/', $snippet)) { + return false; + } + return phutil_is_utf8($snippet); } diff --git a/src/applications/files/document/PhabricatorJSONDocumentEngine.php b/src/applications/files/document/PhabricatorJSONDocumentEngine.php new file mode 100644 --- /dev/null +++ b/src/applications/files/document/PhabricatorJSONDocumentEngine.php @@ -0,0 +1,59 @@ +getName())) { + return 2000; + } + + if ($ref->isProbablyJSON()) { + return 1750; + } + + return 500; + } + + protected function newDocumentContent(PhabricatorDocumentRef $ref) { + $raw_data = $this->loadTextData($ref); + + try { + $data = phutil_json_decode($raw_data); + + if (preg_match('/^\s*\[/', $raw_data)) { + $content = id(new PhutilJSON())->encodeAsList($data); + } else { + $content = id(new PhutilJSON())->encodeFormatted($data); + } + + $message = null; + $content = PhabricatorSyntaxHighlighter::highlightWithLanguage( + 'json', + $content); + } catch (PhutilJSONParserException $ex) { + $message = $this->newMessage( + pht( + 'This document is not valid JSON: %s', + $ex->getMessage())); + + $content = $raw_data; + } + + return array( + $message, + $this->newTextDocumentContent($content), + ); + } + +}