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
2. CAJUN
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.

2. CAJUN

          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 www.json.org/example.html 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.
Results
        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 https://github.com/lijoantony/JsonBenchmarkCpp. Feel free to fork away.

Conclusion

          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.

9 comments:

  1. I have recently initiated an open source C++ JSON parser/generator, named rapidjson. Here are my results compared with YAJL and JsonCpp.

    https://code.google.com/p/rapidjson/wiki/Performance

    I failed to test libjson as it cannot operate on my UTF-8 test data, and its macro with unicode macro cannot compile properly.

    ReplyDelete
  2. Likewise, my library cJSON consistently outperforms libjson, although it's quite lightweight (and only involves compiling a single file into a project). I hacked up a quick test of your testharness for cJSON and I'm seeing cJSON parse about 3x speed of libjson, and print at 2x speed.

    ReplyDelete
  3. @Dave
    I had seen many good reviews and recommendations for cJSON. But I couldn't include cJSON in my evaluation as I was more focused on a json library with a C++ interface, which C++ programmers would be more comfortable with. But I must say that your numbers could persuade even hard core C++ programmers. Good work!

    ReplyDelete
  4. There's a problem with json_spirit when dealing with exotic characters, like \u00e5 (å). You can't access it like \x00\xE5 (or \xE5\x00) in your output, it just decodes into \xEF, which is completely broken...

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. Thanks for sharing your sources! I did the test on my machine, and found out that default cmake doesnt build json_spirit with optimized flags. So I added "set(CMAKE_BUILD_TYPE Release)"

    Here are results:

    #library parsing writing
    cajun 2226 152
    json_spirit 1007 365
    libjson 115 155

    ReplyDelete
    Replies
    1. Ummmm, my bad. cajun wasnt called with -02 on my test above. So here the final results with a bigger json!!

      #library parsing writing
      cajun 213803 44700
      json_spirit 367842 94634
      libjson 36688 75204

      Delete
  7. @Antonin
    Thanks for sharing your results.

    However there is one drawback with libjson though. Built in UTF-8 support is not present, which could make things difficult.

    ReplyDelete