isagalaev / ijson

Iterative JSON parser with Pythonic interface
http://pypi.python.org/pypi/ijson/
Other
615 stars 134 forks source link

api for async use #43

Closed nlevitt closed 8 years ago

nlevitt commented 9 years ago

Would like to have an api that can be used for example with asyncio. https://github.com/kashifrazzaqui/json-streamer can be used that way (but it doesn't "yield native Python objects out of a JSON stream located under a prefix"). Here's what one approach would look like:

def callback(item):
    # do something...

parser = ijson.Parser()
parser.add_item_listener(prefix, callback)
parser.consume(chunk1)
parser.consume(chunk2)
# etc
parser.close()
isagalaev commented 8 years ago

Took me a while to get to it, sorry!

The point of ijson was to go away from the callback-based API towards a more natural looking iterator, so the approach you're describing is not for ijson. I laid out my thoughts and current state of affairs in a similar ticket #45 (closing this one as a dupe, then).