Description
Hi. First of all thank you for your work on this library.
Reading the documentation I understand that the usage suggested by this library is to open a .jsonl
file with jsonlines.open
, or having a list of JSON encoded strings, and then handle the content with utilities.
My usecase would be to take a list (or a generator) of arbitrary jsonable python objects, then return a generator of jsonlines. This behavior does not seem to be supported right now:
>>> import jsonlines
>>> objects = ({"foo": "bar"} for _ in range(100))
>>> reader = jsonlines.Reader(objects)
>>> list(reader)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/azmeuk/dev/yaal/lum1/local.virtualenv/lib/python3.9/site-packages/jsonlines/jsonlines.py", line 203, in iter
yield self.read(
File "/home/azmeuk/dev/yaal/lum1/local.virtualenv/lib/python3.9/site-packages/jsonlines/jsonlines.py", line 162, in read
value = self._loads(line)
File "/usr/lib/python3.9/json/__init__.py", line 339, in loads
raise TypeError(f'the JSON object must be str, bytes or bytearray, '
TypeError: the JSON object must be str, bytes or bytearray, not dict
the JSON object must be str, bytes or bytearray, not dict
I would have imagined something like this:
>>> objects = ({"foo": "bar"} for _ in range(100))
>>> for line in jsonlines.somefunction(objects):
... print(line)
{'foo': 'bar'}
{'foo': 'bar'}
...
This would be very convenient to stream huge jsonlines quantities in a Flask response for instance:
return flask.Response(jsonlines.somefunction(objects), mimetype="application/jsonl")
I suggest implementing such a feature on this library. I volunteer to provide a patch for this if you are not interested in doing it yourself.
What do you think? Do you have preferences on how you would like this to be implemented?
Activity