class Json
Same name in this branch
- 11.1.x vendor/symfony/validator/Constraints/Json.php \Symfony\Component\Validator\Constraints\Json
- 11.1.x vendor/squizlabs/php_codesniffer/src/Reports/Json.php \PHP_CodeSniffer\Reports\Json
- 11.1.x core/lib/Drupal/Component/Serialization/Json.php \Drupal\Component\Serialization\Json
@no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
@internal This class is not covered by the backward compatibility promise for PHPUnit
Hierarchy
- class \PHPUnit\Util\Json
Expanded class hierarchy of Json
1 file declares its use of Json
- JsonMatches.php in vendor/
phpunit/ phpunit/ src/ Framework/ Constraint/ JsonMatches.php
27 string references to 'Json'
- CheckPlatformReqsCommand::printTable in vendor/
composer/ composer/ src/ Composer/ Command/ CheckPlatformReqsCommand.php - ConfigCommand::execute in vendor/
composer/ composer/ src/ Composer/ Command/ ConfigCommand.php - DiagnoseCommand::checkPlatform in vendor/
composer/ composer/ src/ Composer/ Command/ DiagnoseCommand.php - Endpoint::__construct in core/
modules/ media/ src/ OEmbed/ Endpoint.php - Endpoint constructor.
- EnvVarProcessor::getEnv in vendor/
symfony/ dependency-injection/ EnvVarProcessor.php - Returns the value of the given variable as managed by the current instance.
File
-
vendor/
phpunit/ phpunit/ src/ Util/ Json.php, line 28
Namespace
PHPUnit\UtilView source
final class Json {
/**
* @throws InvalidJsonException
*/
public static function prettify(string $json) : string {
$decodedJson = json_decode($json, false);
if (json_last_error()) {
throw new InvalidJsonException();
}
return json_encode($decodedJson, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
}
/**
* To allow comparison of JSON strings, first process them into a consistent
* format so that they can be compared as strings.
*
* @return array ($error, $canonicalized_json) The $error parameter is used
* to indicate an error decoding the json. This is used to avoid ambiguity
* with JSON strings consisting entirely of 'null' or 'false'.
*/
public static function canonicalize(string $json) : array {
$decodedJson = json_decode($json);
if (json_last_error()) {
return [
true,
null,
];
}
self::recursiveSort($decodedJson);
$reencodedJson = json_encode($decodedJson);
return [
false,
$reencodedJson,
];
}
/**
* JSON object keys are unordered while PHP array keys are ordered.
*
* Sort all array keys to ensure both the expected and actual values have
* their keys in the same order.
*/
private static function recursiveSort(mixed &$json) : void {
// Nulls, empty arrays, and scalars need no further handling.
if (!$json || is_scalar($json)) {
return;
}
$isObject = is_object($json);
if ($isObject) {
// Objects need to be sorted during canonicalization to ensure
// correct comparsion since JSON objects are unordered. It must be
// kept as an object so that the value correctly stays as a JSON
// object instead of potentially being converted to an array. This
// approach ensures that numeric string JSON keys are preserved and
// don't risk being flattened due to PHP's array semantics.
// See #2919, #4584, #4674
$json = (array) $json;
ksort($json, SORT_STRING);
}
foreach ($json as &$value) {
self::recursiveSort($value);
}
if ($isObject) {
$json = (object) $json;
}
}
}
Members
Title Sort descending | Modifiers | Object type | Summary |
---|---|---|---|
Json::canonicalize | public static | function | To allow comparison of JSON strings, first process them into a consistent format so that they can be compared as strings. |
Json::prettify | public static | function | |
Json::recursiveSort | private static | function | JSON object keys are unordered while PHP array keys are ordered. |