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 decode( $json)
 
Decode JSON data that may contain path based references.

  • deprecated 9.16.0.0This method will be removed in Highlight.php. Make use of `decodeRef` instead.
  • return mixed The decoded JSON data
public decodeRef( $json)
 
Decode JSON data that may contain path based references.

  • return void
Properties
private $paths
 
Array to hold all data paths in the given JSON data.

  • var array
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.

  • return void
private resolvePathReferences( $s, $limit = 20, $depth = 1)
 
Recurse through the data tree and resolve all path references.

  • return void
© 2020 Bruce Wells
Search Namespaces \ Classes
ConfigurationNumbers (0-9.) only