Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F15312087
D19255.id46099.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
3 KB
Referenced Files
None
Subscribers
None
D19255.id46099.diff
View Options
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 @@
+<?php
+
+final class PhabricatorJSONDocumentEngine
+ extends PhabricatorTextDocumentEngine {
+
+ const ENGINEKEY = 'json';
+
+ public function getViewAsLabel(PhabricatorDocumentRef $ref) {
+ return pht('View as JSON');
+ }
+
+ protected function getDocumentIconIcon(PhabricatorDocumentRef $ref) {
+ return 'fa-database';
+ }
+
+ protected function getContentScore(PhabricatorDocumentRef $ref) {
+ if (preg_match('/\.json\z/', $ref->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),
+ );
+ }
+
+}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Fri, Mar 7, 1:49 PM (1 w, 6 d ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
7332784
Default Alt Text
D19255.id46099.diff (3 KB)
Attached To
Mode
D19255: Add a JSON document rendering engine
Attached
Detach File
Event Timeline
Log In to Comment