Copied!

Class to decode JSON data that contains path-based references.

The language data file for highlight.js are written as JavaScript classes and therefore may contain variables. This allows for inner references in the language data. This kind of data can be converterd to JSON using the path based references. This class can be used to decode such JSON structures. It follows the conventions for path based referencing as used in dojox.json.ref form the Dojo toolkit (Javascript). A typical example of such a structure is as follows:

{
  "name":"Kris Zyp",
  "children":[{"name":"Jennika Zyp"},{"name":"Korban Zyp"}],
  "spouse":{
    "name":"Nicole Zyp",
    "spouse":{"$ref":"#"},
    "children":{"$ref":"#children"}
  },
  "oldestChild":{"$ref":"#children.0"}
}

Usage example:

$jr = new JsonRef();
$data = $jr->decode(file_get_contents("data.json"));
echo $data->spouse->spouse->name; // echos 'Kris Zyp'
echo $data->oldestChild->name; // echos 'Jennika Zyp'
  • todo In Highlight.php 10.x, mark this class final with a keyword.
  • since 9.16.0.0Class has been marked as final
  • final
  • internal
CloneableInstantiable
Methods
public decodeRef( $json)
 

Decode JSON data that may contain path based references.

  • param object $json JSON data string or JSON data object
  • return void
Properties
private $paths = NULL
 

Array to hold all data paths in the given JSON data.

  • var array<string,mixed>
Methods
private getPaths( $s, $r = '#')
 

Recurse through the data tree and fill an array of paths that reference the nodes in the decoded JSON data structure.

  • param mixed $s Decoded JSON data (decoded with json_decode)
  • param string $r The current path key (for example: '#children.0').
  • return void
private resolvePathReferences( $s, $limit = 20, $depth = 1)
 

Recurse through the data tree and resolve all path references.

  • param mixed $s Decoded JSON data (decoded with json_decode)
  • param int $limit
  • param int $depth
  • return void
© 2024 Bruce Wells
Search Namespaces \ Classes
Configuration