class JsonBrowser implements IteratorAggregate, Countable

Helper class for working with JSON-encoded data

Constants

OPT_NONEXISTENT_EXCEPTIONS

Throw exceptions instead of using NULL for nonexistent children & siblings

OPT_GET_VALUE

Get node value instead of JsonBrowser object for __get()

OPT_DECODE

Treat the document definition passed to the constructor as JSON, and decode it

OPT_CAST

Cast values with a non-matching type requirement, instead of throwing an exception

OPT_DEFAULT

Default config options [none]

ERR_DECODING_ERROR

Error decoding JSON data

ERR_UNKNOWN_TYPE

Encountered an unknown type

ERR_UNKNOWN_CHILD

Unknown child

ERR_UNKNOWN_SIBLING

Unknown sibling

ERR_UNKNOWN_SELF

Unknown self

ERR_INVALID_CONTAINER_TYPE

Invalid container type

ERR_UNKNOWN_TARGET

Unknown target

ERR_INVALID_TYPE

The value type does not match the provided type mask

ERR_UNDEFINED_CAST

Cannot cast to undefined

TYPE_NULL

NULL type

TYPE_BOOLEAN

Boolean type

TYPE_STRING

String type

TYPE_NUMBER

Number type

TYPE_INTEGER

Integer type (subset of TYPE_NUMBER)

TYPE_ARRAY

Array type

TYPE_OBJECT

Object type

TYPE_UNDEFINED

Undefined type

TYPE_ALL

All defined types

Methods

__construct(int $options = self::OPT_DEFAULT, mixed $document = null)

Create a new instance

__get(JsonBrowser|mixed $key)

Dynamically get child nodes or values as object properties

__set(mixed $key, mixed $value)

Dynamically set child node values as object properties

asRoot()

Get the current node as a document root

attach(mixed $document)

Attach to an existing decoded document

bool
childExists(mixed $key)

Check whether a child element exists

int
count()

Count the number of children contained within this node

deleteValue(bool $deleteEmpty = false)

Delete the current node value

deleteValueAt(string $path, bool $deleteEmpty = false)

Delete the value at a given path

mixed
getAnnotation(string $name)

Get single node annotation

array
getAnnotations(string $name = null)

Get node annotations

getChild(mixed $key)

Get a child node

getIterator()

Get an iterator handle

string
getJSON(int $options = \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE)

Get the JSON source for the current node

mixed
getKey()

Get the node index key (i.e. the child name within the parent node)

getNodeAt(string $path)

Get the node at a given path

getParent()

Get parent node

string
getPath()

Get the node path

getRoot()

Get root node

getSibling(mixed $key)

Get a sibling node

int
getType(bool $onlyOne = false)

Get the document value type

mixed
getValue(int $asType = 0, bool $cast = null)

Get the node value

mixed
getValueAt(string $path, int $asType = 0, bool $cast = null)

Get the value at a given path

bool
isEqualTo(JsonBrowser|mixed $value)

Test whether the document value is equal to a given value

bool
isNotType(int $types)

Test whether the document value is not of a given type

bool
isRoot()

Test whether the current node is the document root

bool
isType(int $types, bool $all = false)

Test whether the document value is of a given type

loadJSON(string $json)

Load document from a JSON string

bool
nodeExists()

Check whether the current node exists in the parent document

setAnnotation(string $name, mixed $value, bool $clear = false)

Set a node annotation

setValue(mixed $value, bool $padSparseArray = false)

Set the node value

setValueAt(string $path, mixed $value, bool $padSparseArray = false)

Set the value at a given path

bool
siblingExists(mixed $key)

Check whether a sibling exists

Details

at line 108
__construct(int $options = self::OPT_DEFAULT, mixed $document = null)

Create a new instance

Parameters

int $options Configuration options (bitmask)
mixed $document Reference to the default document

at line 125
__get(JsonBrowser|mixed $key)

Dynamically get child nodes or values as object properties

Parameters

JsonBrowser|mixed $key Index key

at line 144
__set(mixed $key, mixed $value)

Dynamically set child node values as object properties

Will set the value on a child node, but will not pad sparse arrays.

Parameters

mixed $key Index key
mixed $value Value data to set

at line 156
JsonBrowser asRoot()

Get the current node as a document root

Return Value

JsonBrowser A new JsonBrowser instance pointing to the current node

at line 195
attach(mixed $document)

Attach to an existing decoded document

Parameters

mixed $document Reference to the target document

at line 208
bool childExists(mixed $key)

Check whether a child element exists

Parameters

mixed $key Index key

Return Value

bool Whether the given child exists

at line 220
int count()

Count the number of children contained within this node

Return Value

int The number of children within this node

at line 236
deleteValue(bool $deleteEmpty = false)

Delete the current node value

Parameters

bool $deleteEmpty Whether to delete empty containers

at line 249
deleteValueAt(string $path, bool $deleteEmpty = false)

Delete the value at a given path

Parameters

string $path JSON pointer to the node that should be deleted
bool $deleteEmpty Whether to delete empty containers

at line 262
mixed getAnnotation(string $name)

Get single node annotation

Parameters

string $name Annotation name

Return Value

mixed Most recently-set annotation matching the given name, or null if not set

at line 276
array getAnnotations(string $name = null)

Get node annotations

Parameters

string $name Annotation name

Return Value

array Array of annotations matching $name, or an associative array of all annotations

at line 289
JsonBrowser getChild(mixed $key)

Get a child node

Parameters

mixed $key Index key

Return Value

JsonBrowser Child node

at line 308
Iterator getIterator()

Get an iterator handle

Return Value

Iterator Iterator instance

at line 321
string getJSON(int $options = \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE)

Get the JSON source for the current node

Parameters

int $options Bitwise options for json_encode()

Return Value

string Encoded JSON string

at line 333
mixed getKey()

Get the node index key (i.e. the child name within the parent node)

Return Value

mixed Index key within parent node, or null if this is the root

at line 346
JsonBrowser getNodeAt(string $path)

Get the node at a given path

Parameters

string $path JSON pointer to the requested node

Return Value

JsonBrowser Node at the target path

at line 365
JsonBrowser|null getParent()

Get parent node

Return Value

JsonBrowser|null Parent node

at line 385
string getPath()

Get the node path

Return Value

string The node path as a JSON pointer

at line 397
JsonBrowser getRoot()

Get root node

Return Value

JsonBrowser Root node

at line 410
JsonBrowser getSibling(mixed $key)

Get a sibling node

Parameters

mixed $key Index key

Return Value

JsonBrowser Sibling node

at line 431
int getType(bool $onlyOne = false)

Get the document value type

Parameters

bool $onlyOne Get only the most specific type, rather than all applicable

Return Value

int Bitmask list of applicable types (See JsonBrowser::TYPE_* constants)

at line 454
mixed getValue(int $asType = 0, bool $cast = null)

Get the node value

Parameters

int $asType Ensure that the returned value matches one of the specified types
bool $cast Whether to cast the value if necessary to ensure the correct type

Return Value

mixed Node value

at line 480
mixed getValueAt(string $path, int $asType = 0, bool $cast = null)

Get the value at a given path

Parameters

string $path JSON pointer to the requested node
int $asType Ensure that the returned value matches one of the specified types
bool $cast Whether to cast the value if necessary to ensure the correct type

Return Value

mixed Value at the given path

at line 509
bool isEqualTo(JsonBrowser|mixed $value)

Test whether the document value is equal to a given value

Comparisons considered equal: - normal strictly-typed equality (===); - loosely-typed equality (==) where both values are numeric and are not strings; - any object having the same number and value for all properties, as per the above tests.

Parameters

JsonBrowser|mixed $value Value to compare against

Return Value

bool Whether the node value is equal to $value

at line 528
bool isNotType(int $types)

Test whether the document value is not of a given type

Parameters

int $types Types to test for

Return Value

bool Whether the type does not match

at line 540
bool isRoot()

Test whether the current node is the document root

Return Value

bool Whether this node is the document root

at line 554
bool isType(int $types, bool $all = false)

Test whether the document value is of a given type

Parameters

int $types Types to test for
bool $all Whether to require all types, or just one

Return Value

bool Whether the type matches

at line 569
loadJSON(string $json)

Load document from a JSON string

Parameters

string $json JSON-encoded document

at line 602
bool nodeExists()

Check whether the current node exists in the parent document

Return Value

bool Whether the current node exists

at line 616
setAnnotation(string $name, mixed $value, bool $clear = false)

Set a node annotation

Parameters

string $name Annotation name
mixed $value Annotation value
bool $clear Clear existing annotations with the same name

at line 629
setValue(mixed $value, bool $padSparseArray = false)

Set the node value

Parameters

mixed $value Data to set
bool $padSparseArray Whether to left-pad sparse arrays with null values

at line 643
setValueAt(string $path, mixed $value, bool $padSparseArray = false)

Set the value at a given path

Parameters

string $path JSON pointer to the requested node
mixed $value Data to set
bool $padSparseArray Whether to left-pad sparse arrays with null values

at line 656
bool siblingExists(mixed $key)

Check whether a sibling exists

Parameters

mixed $key Index key

Return Value

bool Whether the sibling exists