In connection with the recent need to work with data in JSON-format, I decided to investigate the issue of existing libraries for working with JSON for C / C ++. . At the moment there are more or less large number of them. Here is a list from json.org ( http://www.json.org ):
C:
Json_checker
Json parser
json-c
M's JSON parser
YAJL
cJSON
Jansson
js0n
Libu
C ++:
jsoncpp
zoolib
JOST
CAJUN
libJSON
nosjob
The main requirements that I put forward for the required library:
Low memory consumption (most JSON parsers, like XML parsers, parse the file into some of their structures in memory, from which you can extract information about the data in the file (a la DOM tree for XML). The bottom line is that the dependence of the size of the memory for these objects is linear on the file itself and upon receipt of a file that is larger than the expected size, we get unexpected growths of memory (and time for allocation / release), and it may happen that memory in such a quantity may be).
The possibility of partial data parsing (i.e. if I have already read the necessary data, why should I parse everything else? For example, I read the message header and that's enough.)
No extra “frills” (such as searching using specialized queries or validation - we’ll confine ourselves to just parsing).
At first I considered cJSON ( http://sourceforge.net/projects/cjson ), but it turned out that it parses the entire document into memory (although it does so with its minimum consumption and quickly enough). As a result, I stopped at yajl ( http://lloyd.github.com/yajl ) - I need to say the only JSON stream parser. Some facts about yajl:
It performs parsing in the SAX style - the developer creates handler methods for specific types of events (object start, key meeting, text value meeting, numeric value meeting, etc.) and transfers their list to the parser, then starts the parser and the parser in Parsing calls these methods with the data that it encounters in the file (you can choose for which events to receive notifications, and for which not).
It allows streaming parsing - data can be passed to parsing in parts (as it is received over the network, generated by other code, or read from disk).
Parsing can be interrupted during parsing.
Memory consumption is minimal (tested by experience: only the necessary minimum is used, which does not depend on the size of the data for parsing).
Speed ​​at the level (due to the minimum of checks performed - only well-form validation).
The ability to generate JSON (performed by about the same methods that receive event notifications).
Documentation on the parser is scarce enough, but a couple of examples on the site and the study of comments in the header files will allow you to understand everything about how to work with this library.
And a few words in conclusion:
For my needs, the library came up, I even learned to use it in the C ++ classes, despite the fact that it uses function references (function pointers).
But Krommer “push” -parser, I would like to know: are there “pull” -parsers (a la StAX or interface “reader” in libxml-2) for JSON?