docteurklein / json-chunks
JSON encoder using generators to stream
1.0.1
2018-09-13 09:49 UTC
Requires (Dev)
- phpspec/phpspec: ^4.3
This package is auto-updated.
Last update: 2024-11-29 05:50:44 UTC
README
What ?
A php library that allows to stream chunks of json documents.
How ?
It takes advantage of php generators and the native json_encode
function.
To start streaming parts of a document, pass a function that yield
serializable values.
Note: You can nest multiple generators together.
Example
$chunks = DocteurKlein\JsonChunks\Encode::from([ '_links' => [ 'product' => function() { yield from [1, 2, 3]; }, ], '_embedded' => [ 'product' => function() { yield from [ 1, 2, function() { yield from [3]; } ]; }, ], ]); foreach ($chunks as $chunk) { echo $chunk; }
Tradeoffs
- This library won't try to pretty print the streamed content. It provides a
pretty
option for debug purposes, but that will buffer the whole output and thus won't stream anymore. - Arrays containing both numeric and string indexes will be encoded to a json object or array depending on the type of the first key.