Download the PHP package xp-forge/json without Composer
On this page you can find all versions of the php package xp-forge/json. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download xp-forge/json
More information about xp-forge/json
Files in xp-forge/json
Package json
Short Description Reads and writes JSON to and from various input sources
License BSD-3-Clause
Homepage http://xp-framework.net/
Informations about the package json
JSON
Reads and writes JSON to and from various input sources.
Examples
Reading can be done from a string using one of the Input
implementations:
Writing can be done to a string or using one of the Output
implementations:
Formatted output
To change the output format, pass a Format
instance to the output's constructor. The formats available are:
DenseFormat($options)
: Best for network I/O, no unsignificant whitespace, default if nothing given and accessible viaFormat::dense($options= ~Format::ESCAPE_SLASHES)
.WrappedFormat($indent, $options)
: Wraps first-level arrays and all objects, uses whitespace after commas colons. An instance of this format using 4 spaces for indentation and per default leaving forward slashes unescaped is available viaFormat::wrapped($indent= " ", $options= ~Format::ESCAPE_SLASHES)
.
The available options that can be or'ed together are:
Format::ESCAPE_SLASHES
: Escape forward-slashes with "\" - default behavior.Format::ESCAPE_UNICODE
: Escape unicode with "\uXXXX" - default behavior.Format::ESCAPE_ENTITIES
: Escape XML entities&
,"
,<
and>
. Per default, these are represented in their literal form.
The above code will yield the following output:
Sequential processing
Processing elements sequentially can save you memory and give a better performance in certain situations.
Reading
You can use the elements()
method to receive an iterator over a JSON array. Instead of loading the entire source into memory and then returning the parsed array, it will parse one array element at a time, yielding them while going.
If you get a huge object, you can also process it sequentially using the pairs()
method. This will parse a single key/value pair at a time.
To detect the type of the data on the stream (again, without reading it completely), you can use the type()
method.
Writing
To write data sequentially, you can use the begin()
method and the stream it returns. This makes sense when the source offers a way to read data sequentially, if you already have the entire data in memory, using write()
has the same effect.
As the Stream
class implements the Closeable interface, it can be used in the with
statement:
Further reading
- Performance figures. TL;DR: While slower than the native functionality, the performance overhead is in low millisecond ranges. Using sequential processing we have an advantage both performance- and memory-wise.
- Parsing JSON is a Minefield. This library runs this test suite next to its own.