class JsonBrowser implements IteratorAggregate

Helper class for working with JSON-encoded data

Constants

OPT_NONEXISTENT_EXCEPTIONS

Throw exceptions instead of using NULL for nonexistent children & siblings

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

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

Methods

__construct(string $json, int $options = 0)

Create a new instance

bool
childExists(mixed $key)

Check whether a child element exists

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()

Get the document value type

mixed
getValue()

Get the document value

mixed
getValueAt(string $path)

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
isType(int $types, bool $all = false)

Test whether the document value is of a given type

bool
nodeExists()

Check whether the current node exists in the parent document

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 90
__construct(string $json, int $options = 0)

Create a new instance

Parameters

string $json JSON-encoded data
int $options Configuration options (bitmask)

at line 126
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 148
JsonBrowser getChild(mixed $key)

Get a child node

Parameters

mixed $key Index key

Return Value

JsonBrowser Child node

at line 173
Iterator getIterator()

Get an iterator handle

Return Value

Iterator Iterator instance

at line 186
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 198
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 211
JsonBrowser getNodeAt(string $path)

Get the node at a given path

Parameters

string $path JSON pointer to the requested node

Return Value

JsonBrowser

at line 239
JsonBrowser|null getParent()

Get parent node

Return Value

JsonBrowser|null Parent node

at line 251
string getPath()

Get the node path

Return Value

string The node path as a JSON pointer

at line 263
JsonBrowser getRoot()

Get root node

Return Value

JsonBrowser Root node

at line 276
JsonBrowser getSibling(mixed $key)

Get a sibling node

Parameters

mixed $key Index key

Return Value

JsonBrowser

at line 294
int getType()

Get the document value type

Return Value

int

at line 336
mixed getValue()

Get the document value

Return Value

mixed

at line 350
mixed getValueAt(string $path)

Get the value at a given path

Parameters

string $path JSON pointer to the requested node

Return Value

mixed Value at the given path

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

Test whether the document value is equal to a given value

Parameters

JsonBrowser|mixed $value Value to compare against

Return Value

bool

at line 382
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 396
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 411
bool nodeExists()

Check whether the current node exists in the parent document

Return Value

bool

at line 429
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 474
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 487
bool siblingExists(mixed $key)

Check whether a sibling exists

Parameters

mixed $key Index key

Return Value

bool Whether the sibling exists