@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.)
hq.recaptime.dev/wiki/Phorge
phorge
phabricator
1<?php
2
3final class PhabricatorConfigJSON extends Phobject {
4 /**
5 * Properly format a JSON value.
6 *
7 * @param mixed $value Any value, but should be a raw value, not a string of
8 * JSON.
9 * @return string
10 */
11 public static function prettyPrintJSON($value) {
12 // If the value is an array with keys "0, 1, 2, ..." then we want to
13 // show it as a list.
14 // If the value is an array with other keys, we want to show it as an
15 // object.
16 // Otherwise, just use the default encoder.
17
18 $type = null;
19 if (is_array($value)) {
20 $list_keys = range(0, count($value) - 1);
21 $actual_keys = array_keys($value);
22
23 if ($actual_keys === $list_keys) {
24 $type = 'list';
25 } else {
26 $type = 'object';
27 }
28 }
29
30 switch ($type) {
31 case 'list':
32 $result = id(new PhutilJSON())->encodeAsList($value);
33 break;
34 case 'object':
35 $result = id(new PhutilJSON())->encodeFormatted($value);
36 break;
37 default:
38 $result = json_encode($value);
39 break;
40 }
41
42 // For readability, unescape forward slashes. These are normally escaped
43 // to prevent the string "</script>" from appearing in a JSON literal,
44 // but it's irrelevant here and makes reading paths more difficult than
45 // necessary.
46 $result = str_replace('\\/', '/', $result);
47 return $result;
48
49 }
50}