Changeset View
Changeset View
Standalone View
Standalone View
src/parser/PhutilJSON.php
| <?php | <?php | ||||
| /** | /** | ||||
| * Utilities for wrangling JSON. | * Utilities for wrangling JSON. | ||||
| * | * | ||||
| * @task pretty Formatting JSON Objects | * @task pretty Formatting JSON Objects | ||||
| * @task internal Internals | * @task internal Internals | ||||
| */ | */ | ||||
| final class PhutilJSON { | final class PhutilJSON extends Phobject { | ||||
| /* -( Formatting JSON Objects )-------------------------------------------- */ | /* -( Formatting JSON Objects )-------------------------------------------- */ | ||||
| /** | /** | ||||
| * Encode an object in JSON and pretty-print it. This generates a valid JSON | * Encode an object in JSON and pretty-print it. This generates a valid JSON | ||||
| * object with human-readable whitespace and indentation. | * object with human-readable whitespace and indentation. | ||||
| * | * | ||||
| * @param dict An object to encode in JSON. | * @param dict An object to encode in JSON. | ||||
| * @return string Pretty-printed object representation. | * @return string Pretty-printed object representation. | ||||
| */ | */ | ||||
| public function encodeFormatted(array $object) { | public function encodeFormatted(array $object) { | ||||
| return $this->encodeFormattedObject($object, 0)."\n"; | return $this->encodeFormattedObject($object, 0)."\n"; | ||||
| } | } | ||||
| /** | |||||
| * Encode a list in JSON and pretty-print it, discarding keys. | |||||
| * | |||||
| * @param list<wild> List to encode in JSON. | |||||
| * @return string Pretty-printed list representation. | |||||
| */ | |||||
| public function encodeAsList(array $list) { | |||||
| return $this->encodeFormattedArray($list, 0)."\n"; | |||||
| } | |||||
| /* -( Internals )---------------------------------------------------------- */ | /* -( Internals )---------------------------------------------------------- */ | ||||
| /** | /** | ||||
| * Pretty-print a JSON object. | * Pretty-print a JSON object. | ||||
| * | * | ||||
| * @param dict Object to format. | * @param dict Object to format. | ||||
| * @param int Current depth, for indentation. | * @param int Current depth, for indentation. | ||||
| ▲ Show 20 Lines • Show All 73 Lines • ▼ Show 20 Lines | /* -( Internals )---------------------------------------------------------- */ | ||||
| private function encodeFormattedValue($value, $depth) { | private function encodeFormattedValue($value, $depth) { | ||||
| if (is_array($value)) { | if (is_array($value)) { | ||||
| if (empty($value) || array_keys($value) === range(0, count($value) - 1)) { | if (empty($value) || array_keys($value) === range(0, count($value) - 1)) { | ||||
| return $this->encodeFormattedArray($value, $depth); | return $this->encodeFormattedArray($value, $depth); | ||||
| } else { | } else { | ||||
| return $this->encodeFormattedObject($value, $depth); | return $this->encodeFormattedObject($value, $depth); | ||||
| } | } | ||||
| } else { | } else { | ||||
| if (defined('JSON_UNESCAPED_SLASHES')) { | |||||
| // If we have a new enough version of PHP, disable escaping of slashes | |||||
| // when pretty-printing values. Escaping slashes can defuse an attack | |||||
| // where the attacker embeds "</script>" inside a JSON string, but that | |||||
| // isn't relevant when rendering JSON for human viewers. | |||||
| return json_encode($value, JSON_UNESCAPED_SLASHES); | |||||
| } else { | |||||
| return json_encode($value); | return json_encode($value); | ||||
| } | } | ||||
| } | } | ||||
| } | |||||
| /** | /** | ||||
| * Render a string corresponding to the current indent depth. | * Render a string corresponding to the current indent depth. | ||||
| * | * | ||||
| * @param int Current depth. | * @param int Current depth. | ||||
| * @return string Indentation. | * @return string Indentation. | ||||
| * @task internal | * @task internal | ||||
| Show All 10 Lines | |||||