Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Json.php

function Json::recursiveSort

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.

1 call to Json::recursiveSort()
Json::canonicalize in vendor/phpunit/phpunit/src/Util/Json.php
To allow comparison of JSON strings, first process them into a consistent format so that they can be compared as strings.

File

vendor/phpunit/phpunit/src/Util/Json.php, line 73

Class

Json
@no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit

Namespace

PHPUnit\Util

Code

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;
    }
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal