json-machine
json-machine copied to clipboard
Push parsing support
Support to incrementally feed the parser via an explicit method call where the pull approach of foreach cannot be used. Useful for example for curl's CURLOPT_WRITEFUNCTION or when receiving json chunks in an event loop.
Proposed usage (implicit):
$items = new PushItems(['pointer' => '/results']);
$callback = function ($jsonChunk) use ($items) {
$items->push($jsonChunk);
foreach($items as $item) {
// process currently available items
}
}
or more explicit (similar to current API):
$queue = new QueueChunks();
$items = Items::fromQueue($queue, ['pointer' => '/results']);
$callback = function ($jsonChunk) use ($items, $queue) {
$queue->push($jsonChunk);
foreach($items as $item) {
// process currently available items
}
}
Any other proposal?