Monday, November 17, 2008

JSON support in native code.

I recently needed to look at JSON support libraries for a project I'm working on. It was required that it be a native C or C++ implementation. Some Boost libraries were available. This is a brief report on what I've found so far.

json-c v0.7 (internally its marked as 0.3) was the first one I looked at, mainly because I had used before in an Objective-C environment.
Building it on OSX was easy, creating a libjson.dylib that I could link to. The programming interface is basic C, creating 'json_object's, and adding them to other 'json_object's and 'json_object_array's. There is an incremental tokenizer/parser, as well as the ability to convert a JSON object into a standard JSON string. Its simple, seems lightweight, and I know it works since I've used it before.

TINYJSON-1.3.0 is the next one I examined. This one is heavily dependent upon the Boost C++ Template Libraries, also pulling in the Boost Spirit parser, that I'm unfamiliar with. This was my first attempt at using Boost on OSX, so I first had to pull it down, build and install it, not a big deal. The TinyJSON distribution included a basic set of Boost TEST_CASE's which took me awhile to figure out how ot configure in XCode. Next hurdle is the programming model - its all based on C++ templates, which is useful but sometimes hard to fathom.
Main problem with it though is that its only defined for reading JSON data, not writing it. I need writing.

JSONCPP is the last one I've tried so far. This one looks promising, but I've had problems just getting it to build - it uses something called 'scons' to manage its build process. After following all the instructions, I had no luck. I tried pulling the project tree into XCode and building it manually, and was able to make that work at least. Object structure looks promising, although it has the usual c++ verbosity. Worse though, are no examples and I find that all of the test code is written in Python, and doens't work. i've probably not built something correctly, I just don't know what yet.

A couple of other libaries that I haven't had time to examine closely yet: Jost and Jaula, the former is C++, the latter strictly C. A closer look at Jost is required, as I see it is also Boost-based and it seems relatively simple.

After this little exercise, I'll probably just use the libjson support - its simple and it works. If/when I get more time, I'll look into the C++ versions again.
I was surprised at the dearth of C++ -based versions, I suppose that this is because libjson support is good enough.

9/2009: I ended up doing this for another project too, and again ran into problems with the Boost-based options. Since this was a C++ project I ended up using the Cajun library, and find that it works pretty well. Its a bit stream-oriented but that can probably be changed if it gets in the way too much, or becomes a performance bottleneck. I found it at