3

I have seen a number of libs for parsing JSON in C but none of them can read and parse directly from file streams. The problem with all such libs e.g Yajl, cjson is that if the json document in the file is huge then you have to first read all of that into a memory buffer and then run the APIs provided by these libs to parse it.

There APIs often look like

cJSON *cJSON_Parse(const char *value)

which take a char* to a buffer.

I want to avoid that since my files can be very large and i donot know the sizes of the files in advance. Moreover, these libs maintain reference to objects,arrays in the actual buffer to retrieve the values so i cannot free the original buffer.

Is there a JSON parsing lib that can read and parse directly from file streams?

Sled
  • 18,541
  • 27
  • 119
  • 168
auny
  • 1,920
  • 4
  • 20
  • 37
  • Look at these, if you haven't yet: [jsonsl](https://github.com/mnunberg/jsonsl), [Jansson](http://www.digip.org/jansson/), [cson](http://fossil.wanderinghorse.net/repos/cson/index.cgi/index), [json-c](https://github.com/json-c/json-c), [json-parser](https://github.com/udp/json-parser), [jsmn](http://zserge.bitbucket.org/jsmn.html). I haven't looked at their code. – Alexey Frunze Oct 05 '12 at 07:01

2 Answers2

3

http://lloyd.github.com/yajl/ is probably what you are looking for

Erich Kitzmueller
  • 36,381
  • 5
  • 80
  • 102
  • Haven't tested it, but +1 since it looks like it applies very well. – unwind Oct 05 '12 at 07:16
  • I mentioned this above in my comment. If you look at its API it operates on char* for a buffer in memory. Not on files – auny Oct 05 '12 at 07:20
  • If you're after the JSON equivalent of a SAX parser for XML, then yajl is what you're after. See the callbacks you would implement: http://lloyd.github.com/yajl/yajl-2.0.1/structyajl__callbacks.html – Alex Reynolds Oct 05 '12 at 08:10
  • Here's what looks like example code that demonstrates the use of yajl as an event-based (SAX) parser: http://lloyd.github.com/yajl/yajl-2.0.1/reformatter_2json_reformat_8c-example.html – Alex Reynolds Oct 05 '12 at 08:17
  • @auny: But you can read the file in small pieces, you don't have to read the whole file into memory at once. – Erich Kitzmueller Oct 05 '12 at 09:02
  • I just solved the issue with a better approach. i mapped(mmap) the file into my process and then used the address for char* manipulation. – auny Oct 05 '12 at 09:34
1

Mapping the file to the process using mmap() and then simply using the address returned to carry out the char* manipulations makes all of them useful and a good way to solve the problem

auny
  • 1,920
  • 4
  • 20
  • 37