Saturday, November 19, 2011

Comparison and benchmark of C++ JSON libraries

              I had to select an open source ++ json library for a c++ project. There are quiet a few libraries available with different set of features and selecting one among them is not an easy job. Each one has got it's own pros and cons. Web is full of comparisons and benchmarks of json libraries for javascript, java, python etc. But I couldn't find a good comparison for C++ which could help me to make a decision. So I decided to do a comparison of the available options to see which one fits best for my purpose.
I based my evaluation on following criteria,

1. Features and ease of use
2. Performance
3. Documentation and community support

          As it is not practical to learn and evaluate all the available ones, I decided to try the most popular ones, which are,

1. json-cpp
3. json_spirit
4. libjson

1. json-cpp

          Json-cpp is the most popular and widely used json library in c++. It has got a good feature set and nice interface, and is very easy to use. But I couldn't successfully integrate it on a 64-bit machine. I tried to get help on the forum but I didn't get any. Despite its popularity, the community is not very active. A lot of questions remains unanswered on the forum, even after long time. Also there are complaints from users like memory corruption, open critical bugs etc. Since I couldn't integrate it on 64-bit machine and I believe community is crucial for any open source project, I am dropping json-cpp from my further analysis.


          Another popular one is Cajun. It's easy to build and integrate, with good interface and features. It comes with a small documentation and a sample program and should not be difficult to get started. But it is not being developed actively  and there has been no release after 2009. Forum is also not active.

3. json_spirit

          Json_spirti is another popular library with a long history. It has got good features and a nice interface. Building and integration was easy. But I noticed the source files take comparatively long time for compilation. It is written using boost spirit parser generator and hence there is a dependency over boost. The source package comes with example programs and one can start right away. This is being actively developed and the support is also good.

4. libjson

          libjson is a super efficient, highly customizable json library with nice features. It provides both C style as well as C++ interface. It comes with some sample programs which would help one to get started in no time. Building and integration was easy. Customization can be done by editing a fully commented header file which should be very easy for any programmer. Another specialty is its well written and comprehensive documentation which is not very common with open source projects. libjson enjoys an active community and forum. The author is fully committed to the project and I was surprised by his quick response to any query in the forum or bug reports.

Performance benchmark

          I wrote a small program to benchmark these libs. I measured the timings for two operations,

1. parsing - Parsing a json string into a library specific json object
2. writing - Converting the library specific json object into a json string

          I have not done any customization of these libraries for extra performance enhancements. I have used an example json string available at as test data. To get accurate results do the same test with your actual json data. I am doing this test on an Ubuntu Linux 64 bit machine with Intel Core i5 2.30 GHZ micro processor.

$ ./JsonBenchmarkCpp > results.dat
$ cat results.dat.
#library                 parsing                  writing                 
cajun                      907                        118                     
json_spirit              8655                      510                     
libjson                    81                          115     

Numbers are the time taken for an operation in micro seconds.
        I used gnuplot to draw a graph out of these numbers. As they say "A picture is worth a thousand words".

I had plot an enlarged graph to see the libjson timings!

      This graph says it all. libjson is the clear winner, followed cajun and json_spirit. libjson can be customized further for higher performance.
Source code and the gnuplot script used for benchmarking can be found at Feel free to fork away.


          No software is perfect. Good ones undergo continuous evolution. A fully committed author and an active community gives and extra edge for libjson in this aspect. It has performed extremely well in my tests. So libjson is the winner in my analysis. I have chosen libjson for my project. However I would like to thank developers of all the libraries for their time and effort.      

Update: 26-11-2011
              I had built libjson with the default settings for the performance testing. By default libjson would not parse the json string completely and hence my numbers were wrong about libjson. So I did the test again with the proper setting and the post has been updated with the new numbers. Still libjson is at least 10 times faster than other libraries I had tested. Thanks to the libjson author who informed me about this mistake in my test.

Friday, November 18, 2011


          Both XML and JSON are competing formats for storage and interchange of data in a machine, language, platform independent way. JSON boasts simplicity as its main advantage over XML. Even though XML is the more established technology, it is being replaced by JSON as the preferred solution in many areas. it's popularity is only growing as more and more developers are choosing JSON over XML as their preferred solution for rpc and data interchange in general. Though JSON had its humble beginning as a simple way of data exchange in java script, currently it's available in all major programming languages and technologies. For any one who would like to learn more about JSON would be a good starting point.
         But there is another side to this story. Internet is full of discussions and arguments, where supporters of both have been trying to establish their technical superiority over the other. You can find many blog posts where someone proves one is better than the other. The choice of one over the other is not so straight forward, and it gets complicated due to the fact that the performance of these technologies for any specific purpose depends on the nature of the data as well. So making a decision based on these blog posts might not be good idea.
         So at work, when we had to decide between json and xml for rpc, we did a thorough evaluation of both options in terms of data transfer efficiency and processing efficiency. Json showed clear advantages over xml for our purpose, and we decided to go with JSON. I can't disclose any specifics of the tests, but it should be very easy for any one to do some tests before deciding JSON or XML as the solution for their problem.