Remove product literal strings in "pht()", part 4
[phabricator.git] / src / applications / config / json / PhabricatorConfigJSON.php
blob0433e4a1dc664f24cb30de5a6539e5f0ee016ebc
1 <?php
3 final class PhabricatorConfigJSON extends Phobject {
4 /**
5 * Properly format a JSON value.
7 * @param wild Any value, but should be a raw value, not a string of JSON.
8 * @return string
9 */
10 public static function prettyPrintJSON($value) {
11 // If the value is an array with keys "0, 1, 2, ..." then we want to
12 // show it as a list.
13 // If the value is an array with other keys, we want to show it as an
14 // object.
15 // Otherwise, just use the default encoder.
17 $type = null;
18 if (is_array($value)) {
19 $list_keys = range(0, count($value) - 1);
20 $actual_keys = array_keys($value);
22 if ($actual_keys === $list_keys) {
23 $type = 'list';
24 } else {
25 $type = 'object';
29 switch ($type) {
30 case 'list':
31 $result = id(new PhutilJSON())->encodeAsList($value);
32 break;
33 case 'object':
34 $result = id(new PhutilJSON())->encodeFormatted($value);
35 break;
36 default:
37 $result = json_encode($value);
38 break;
41 // For readability, unescape forward slashes. These are normally escaped
42 // to prevent the string "</script>" from appearing in a JSON literal,
43 // but it's irrelevant here and makes reading paths more difficult than
44 // necessary.
45 $result = str_replace('\\/', '/', $result);
46 return $result;