Search code examples
cjsonparsingyajl

how to use c yajl for streaming


I have been playing with yajl for a couple of days and really like the tree node model. After it is done parsing you get the structure of your json file and you can go through it. Like the example here:

http://lloyd.github.io/yajl/yajl-2.0.1/example_2parse_config_8c-example.html

This example is nice and easy to understand, but I would like to know how to use the streaming example which they show here. http://lloyd.github.io/yajl/yajl-2.0.1/reformatter_2json_reformat_8c-example.html This would be better for large files I imagine.

The streaming example goes through and raises events whenever the parser encounters certain things. Like when it encounters a boolean it raises an event that it found a boolean and I am given the boolean value. However, I dont know what key, or object that boolean belongs to. Likewise, the map close event happens. What can I do there? The map closed, great. Ive got this yajl_gen pointer and I dont know what to do with it. Apologies if this question is vague or i just dont know enough about yajl parsing. Thanks for reading.


Solution

  • Basically, you create your callbacks for each event, and then in those callbacks you can gradually build an hierarchy of data structures and fill them with data contained in JSON stream. Map start/end make up a JSON object. Most of the time, anything between these two events is an associative array (key/value pairs). The key is a string, and it is marked by a map key event. The value could be a string, a number, a boolean, an array (start/end array event), or even another object (start/end map). An so on, and so forth. Each of these is marked by a corresponding event, such as start/end array event, a string event, a number event, or a boolean event. The events come one by one, gradually, as the JSON stream is being processed, and allow you to build whatever hierarchy of data structures you may want.