share
Stack OverflowWhat's the best C++ JSON parser?
[+251] [8] Sam Baker
[2008-10-29 06:37:30]
[ c++ json parsing ]
[ http://stackoverflow.com/questions/245973/whats-the-best-c-json-parser ] [DELETED]

I've seen the C++ JSON links on the official JSON site [1] and would like some feedback on which parser people prefer - for reliability, speed and ease of use.

Pick one from json.org - bobobobo
[+91] [2008-10-29 09:05:43] mpdaly

I looked at most of them a year or so ago, and settled on JsonCpp (http://jsoncpp.sourceforge.net/). I've found it reliable, fast and easy to use, so 3/3.


(1) I'm using jsoncpp also. it requires a little clean-up on my (work) system so integrations for new revisions are a little harder - George Godik
(1) Doesn't appear to work on Linux (amd64) -- the library compiles, but fails to parse anything. - Jyaan
(11) had trouble getting their fancy build system to work on vc90, gave up - Dustin Getz
(4) Workaround for VC90 (and at least for Qt) is to just add those three *.cpp files to your project. - Virne
(3) Also, they have a amalgamate.py script which creates a single .h/.cpp pair of files from all the sources, so you can just drop those into your project to reduce clutter. - reflog
(1) Lack of built-in Unicode support makes JsonCpp useless to me. - Brian Gillespie
(3) Care to elaborate the lack of Unicode support? I tried passing UTF8 string to and from jsoncpp and it seems to maintain them. It also parse \uXXXX input correctly into UTF8. - Stephen Chu
(1) @Reflog where is amalgamate? - Deniz
1
[+59] [2008-10-29 17:25:14] therefromhere

I'm using JSON Spirit [1] on a project at the moment, I'm impressed with it so far.

Note that it does rely on Boost (if only for headers).

Handy features:

  • Has Unicode support.
  • Uses std::vector to hold Arrays which helps interoperability.
  • Provides a pretty print function (write_formatted).
  • Has read and write functions for strings and streams.

Note that Objects are also implemented using vector [2] (not map), which means slower access, but it does mean that the order of elements is maintained.

[1] http://www.codeproject.com/KB/recipes/JSON_Spirit.aspx
[2] http://www.codeproject.com/KB/recipes/JSON_Spirit.aspx?msg=2780996#xx2780996xx

(7) Boost is (mostly) only headers: boost.org/doc/libs/1_36_0/more/getting_started/… - mpdaly
(2) Yeah, but the reason I mention it is that you need to link against Boost Threads if you want to use JSON Spirit in multiple threads. - therefromhere
(15) Just a small note, but it looks like newer versions (after 4.0) are able to store objects in both vectors or maps (configurable as a template parameter). - Magnus Österlind
How fast is it exactly? - the_drow
The source code to JSON Spirit is behind logon ... - stefanB
Why couldn't the author have written json_spirit so that elements can be indexed as both? Some new structure with operator[] and map semantics? - JDonner
I used to love it in earlier versions (<4), but now I found it to considerably bloat my .pdb files, and to generate lots and lots of template warnings when using /W4 with VC++2K8. - jv42
(1) FWIW json-spirit is now available without logon from gitorious.org/json-spirit - boycy
(1) God the generated library is 27m.... steer clear. stripped its 8.7 megs. Even if a lot of that is dead code it is so not worth including. - Hassan Syed
(7) I found JSON Spirit to be a thermonuclear reactor while one often just want simple JSON parsing. The API is cumbersome and build/preprocess time implied by Spirit is insane. - Aurélien Vallée
(1) @AurélienVallée agreed, it is pretty crazy (I've not used more recent versions). Also in retrospect the whole concept of Spirit is pretty nuts. - therefromhere
Is it true that spirit is the only one that can convert classes, vectors etc into json? All other complaints be damned! - user1382306
2
[+26] [2009-12-11 10:39:05] Marty McFly

See also Boost property_tree included in the 1.41.0 release of Boost:

http://www.boost.org/doc/libs/1_41_0/doc/html/property_tree.html [1]

Basically it provides a generic property tree structure and includes parses/generators for JSON, XML and INI. It is header only and it uses Boost Spirit for generation/parsing.

[1] http://www.boost.org/doc/libs/1%5F41%5F0/doc/html/property%5Ftree.html

(14) Boost property_tree does not fully support all type information. From the documentation: JSON values are mapped to nodes containing the value. However, all type information is lost; numbers, as well as the literals "null", "true" and "false" are simply mapped to their string form. - Yukiko
property_tree only parses json information for property trees. - chila
3
[+16] [2011-11-05 08:29:56] Suren

Jansson - http://www.digip.org/jansson/

  1. works nice
  2. seems to be actively maintained as well.
  3. detailed documentation

Just made my life easy.


+1 for Jansson. I really like the path syntax for packing & validating complex data. - Valentin Milea
(3) Hello I see that Jansson is a C Library not a C++ Library - peplamb
4
[+10] [2010-10-20 15:28:33] Spike

I know you are asking about C++, but yajl [1] is definitely worth trying as well. It has many features that are not available from many other parsers, and most importantly, it's fast.

[1] http://lloyd.github.com/yajl/

(2) Could you elaborate on features it has over alternatives. I am looking for a C only JSON parser, saw yacl and am interested in knowing how it differs from competition. - StaxMan
(3) We choose between Json-c and Yajl finally. Json-C is considered because our partner use it as their JSON library. First, I can say that they all works, and we don't want any external dependencies, so C based library will be the choice. Most C++ ones use Boost. After doing some simple testing programs, we chose Yajl anyway, since some implementations in Json-C we don't really like, e.g. you need to allocate the buffer yourself and gave it to json-c, otherwise, json-c won't parse it, and error message is horrible. - Spike
(1) Yajl is beautiful - it's simple and fast, compared to other pure C parsers. - Cyberax
5
[+5] [2010-07-08 11:16:03] BarackBush

I just gave JSON Spirit (the header-only version) a try, because I was already using Boost in my project and JSON Spirit only depends on Boost.

However, I think the documentation (especially for the new header-only version) is not adequate at the moment. Maybe this will change.


6
[+2] [2010-12-08 21:34:32] grundprinzip

jsoncpp builds, but it does not look like darwin-osx is a maintained platform. Even though scons might be an interesting build system the whole setup needs some love.

Tests passing on Snow Leopard 10.6 with

scons platform=linux-gcc


Getting SCons up and running is not really much. I use Brew as a command line package manager, so 'brew install scones' does it. The output file names are a bit odd for jsoncpp, though, including mention of linux. - akauppi
7
[+2] [2011-03-15 05:08:49] Jason Denton

Try the UniversalContainer class. http://greatpanic.com/code.html. It provides a container that acts like a Php/Python/Perl variable that can be a hashmap, array, scalar, or whatever it needs to be. Additional routines allow the container to be serialized and deserialized to JSON.


8