iter icon indicating copy to clipboard operation
iter copied to clipboard

Add a json_encode

Open Neirda24 opened this issue 3 years ago • 2 comments

I don't know if it would be relevant or not. Some benchmark might be required to determine whether or not the original json_encode would fail on big arrays with big objects. If so using iterators could become useful. Comparing to the original one there would be the need for special const such as FORCE_OBJECT(which already exists) and a FORCE_LIST

Neirda24 avatar Jun 13 '21 20:06 Neirda24

I think this would be out of scope. Effectively this calls for a streaming JSON encoder, as you'd probably want not just the input to be an iterator, but the output to be incremental as well. A quick google turns up https://github.com/violet-php/streaming-json-encoder, though I have no further familiarity with that library.

nikic avatar Jun 13 '21 21:06 nikic

Thank you for this library. I was not talking about streaming per se (Already did something using the StreamResponse + IteratorAgreggate + __invoke) but rather a generic iterable->toJsonString taht we could then store for example in a database or else. I don't know if there are usecase where json_encode(iterator_to_array($myIterator)) would create a massive overhead in memory that we could avoid with this new function.

Neirda24 avatar Jun 14 '21 07:06 Neirda24