Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/config/json/PhabricatorConfigJSON.php
12262 views
1
<?php
2
3
final class PhabricatorConfigJSON extends Phobject {
4
/**
5
* Properly format a JSON value.
6
*
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.
16
17
$type = null;
18
if (is_array($value)) {
19
$list_keys = range(0, count($value) - 1);
20
$actual_keys = array_keys($value);
21
22
if ($actual_keys === $list_keys) {
23
$type = 'list';
24
} else {
25
$type = 'object';
26
}
27
}
28
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;
39
}
40
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;
47
48
}
49
}
50
51