I just implemented callback based streaming for jsonyx in the C extension. commit 472bd50
$ pip install git+https://github.com/nineteendo/jsonyx
Is this something you would like to see in the stdlib? (json.dump is implemented entirely in Python)
Processing this in a loop is a lot more complicated though:
>>> import jsonyx as json
>>> from itertools import count
>>> def make_consumer(obj, **kwargs):
... def consumer(make_gen):
... class Writer:
... def write(self, chunk):
... gen.send(chunk)
... gen = make_gen()
... next(gen)
... try:
... json.dump(obj, Writer(), stream=True, **kwargs)
... finally:
... gen.close()
... return consumer
...
>>> @make_consumer([1, 2, 3])
... def _():
... for i in count():
... chunk = yield
... print(f"chunk {i}: {chunk!r}")
...
chunk 0: '['
chunk 1: '1'
chunk 2: ', '
chunk 3: '2'
chunk 4: ', '
chunk 5: '3'
chunk 6: ']'
chunk 7: '\n'
Sidenote: neither jsonyx or the stdlib support reading a file incrementally.