Fastest JSON reader/writer for C++

114,695

Solution 1

http://lloyd.github.com/yajl/

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

Don't really know how they compare for speed, but the first one looks like the right idea for scaling to really big JSON data, since it parses only a small chunk at a time so they don't need to hold all the data in memory at once (This can be faster or slower depending on the library/use case)

Solution 2

rapidjson is a C++ JSON parser/generator designed to be fast and small memory footprint.

There is a performance comparison with YAJL and JsonCPP.


Update:

I created an open source project Native JSON benchmark, which evaluates 29 (and increasing) C/C++ JSON libraries, in terms of conformance and performance. This should be an useful reference.

Solution 3

https://github.com/quartzjer/js0n

Ugliest interface possible, but does what you ask. Zero allocations.

http://zserge.com/jsmn.html Another zero-allocation approach.

The solutions posted above all do dynamic memory allocation, hence will be inevitably end up slower at some point, depending on the data structure - and will be dangerous to include in a heap constrained environment like an embedded system.

Benchmarks of vjson, rapidjson and sajson here : http://chadaustin.me/2013/01/json-parser-benchmarking/ if you are interested in that sort of thing.

And to answer your "writer" part of the question i doubt that you could beat an efficient

printf("{%s:%s}",name,value)

implementation with any library - assuming your printf/sprintf implementation itself is lightweight of course.

EDIT: actually let me take that back, RapidJson allows on-stack allocation only through its MemoryPoolAllocator and actually makes this a default for its GenericReader. I havent done the comparison but i would expect it to be more robust than anything else listed here. It also doesnt have any dependencies, and it doesnt throw exceptions which probably makes it ultimately suitable for embedded. Fully header based lib so, easy to include anywhere.

Share:
114,695
user424060
Author by

user424060

Updated on July 09, 2022

Comments

  • user424060
    user424060 almost 2 years

    I need a C++ JSON parser & writer. Speed and reliability are very critical, I don't care if the interface is nice or not, if it's Boost-based or not, even a C parser is fine (if it's considerably faster than C++ ones).

    If somebody has experience with the speed of available JSON parsers, please advise.

    • Benoît
      Benoît over 13 years
      How can you not care about the niceness of a library API ?
    • user424060
      user424060 over 13 years
      I do care about niceness of API but most of the libs avaibale tend to sacrifice speed when they try to make interface nice and easy to use. For my porject speed is one of the most critical requirement.
    • DevSolar
      DevSolar over 11 years
      I understand the sentiment. But I would always chose the best-supported / best-designed library first, and build a prototype of it, to check if the JSON implementation is on the critical path at all, and whether any sacrifices in terms of readability / maintainability actually make sense. Measure, optimize, measure.
    • James McLaughlin
      James McLaughlin about 11 years
      There's a benchmark here: github.com/lijoantony/JsonBenchmarkCpp
    • Milo Yip
      Milo Yip about 9 years
      There is a benchmark with 28 C/C++ JSON libraries: github.com/miloyip/nativejson-benchmark
    • Kyle Strand
      Kyle Strand over 8 years
      This might be a better fit at Software Recommendations.
    • Ed S.
      Ed S. about 7 years
      @user424060: I know this is old, but that mentality is why programs today run at best as fast as their 1990 equivalents
  • dalle
    dalle almost 11 years
    rapidjson, except for all the preprocessing macros and the call to setjmp it seems like a nice library.
  • Martijn Mellens
    Martijn Mellens over 10 years
    thanks! Now I got from a 7+ seconds parsing time in JsonCpp to 1.5373.
  • Milo Yip
    Milo Yip over 8 years
    @dalle Just as an update, setjmp has been removed for a few years. Thank you for your comment anyway.