Download the PHP package webdevcraft/jsonreader without Composer
On this page you can find all versions of the php package webdevcraft/jsonreader. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download webdevcraft/jsonreader
More information about webdevcraft/jsonreader
Files in webdevcraft/jsonreader
Package jsonreader
Short Description JSON streaming reader (memory safety parser) with chunking option
License MIT
Informations about the package jsonreader
JsonReader
JSON streaming reader (memory safety parser) with chunking option
Purpose and Advantages
- Memory safety read of huge JSON files: solve "out of RAM" problem. Successfully tested with 1.5Gb JSON files
- Possibility to split large JSON file to smaller JSON chunks
- Custom traverse through JSON tree
- Read any JSON source: file, string, resource, character iterator
- Installation with Composer, no need any extra PHP extensions
Requirements
PHP 5.6, 7.0
Installation
With Composer:
Basic Usage
JSON Reader iterates through JSON structure elements
Each iterates to next element until it returns false when finished
So common usage is:
Reader provides information on each element:
-
DEPTH in JSON tree starting from 1:
-
STATE (type of element): . There is syntax sugar issers exist for each of states. State could be one of:
-
Object Start: _JsonReaderInterface::STATE_OBJECTSTART (or )
-
Object Key: _JsonReaderInterface::STATE_OBJECTKEY (or )
-
Object End _JsonReaderInterface::STATE_OBJECTEND (or )
-
Array Start _JsonReaderInterface::STATE_ARRAYSTART (or )
-
Array End _JsonReaderInterface::STATE_ARRAYEND (or )
- Value _JsonReaderInterface::STATEVALUE (or )
-
-
VALUE of element by . Types:
-
For the type of value is automatically casted to PHP type: string, int, float, boolean, null
-
For state is always string
- For other states value is always null
-
Example of elements sequence for test.json is presented in JsonReaderTest.php::testReadTokens()
Chunks Usage
In case if your code is already dependent on JSON string denormanization you could use JsonReader library for chunking of huge file onto smaller JSON strings
Just start buffering chunk on element position you wish by and get chunk by . That returnes JSON chunk string and flushes buffer.
As chunk is buffered in memory make sure your JSON schema guarantees acceptable size of chunk
That's nice practice to wrap chunks retrieve to iterator
Example of test.json chunking is presented in JsonReaderTest.php::testChunks()
Factory Usage
To create reader you have to use JsonReaderFactory
Example of JsonReader creating:
Factory supports any sources by such methods:
- createByFilePath by file path in local filesystem
- createByString by JSON string passing
- createByResource by opened resource link, for example with fopen()
- createByCharacterTraversable - if options above do not suite your needs feel free to write your custom characters iterator using any source you have to deal with. For example you could iterate PSR-7 Message\StreamInterface::read($acceptableSize) and iterate characters inside each of read()