Path: blob/master/src/applications/config/json/PhabricatorConfigJSON.php
12262 views
<?php12final class PhabricatorConfigJSON extends Phobject {3/**4* Properly format a JSON value.5*6* @param wild Any value, but should be a raw value, not a string of JSON.7* @return string8*/9public static function prettyPrintJSON($value) {10// If the value is an array with keys "0, 1, 2, ..." then we want to11// show it as a list.12// If the value is an array with other keys, we want to show it as an13// object.14// Otherwise, just use the default encoder.1516$type = null;17if (is_array($value)) {18$list_keys = range(0, count($value) - 1);19$actual_keys = array_keys($value);2021if ($actual_keys === $list_keys) {22$type = 'list';23} else {24$type = 'object';25}26}2728switch ($type) {29case 'list':30$result = id(new PhutilJSON())->encodeAsList($value);31break;32case 'object':33$result = id(new PhutilJSON())->encodeFormatted($value);34break;35default:36$result = json_encode($value);37break;38}3940// For readability, unescape forward slashes. These are normally escaped41// to prevent the string "</script>" from appearing in a JSON literal,42// but it's irrelevant here and makes reading paths more difficult than43// necessary.44$result = str_replace('\\/', '/', $result);45return $result;4647}48}495051