Fork me on GitHub
Home

JSON frameworks API comparison

10 March 2011

There are dozens of iOS compatible JSON frameworks aka parsers. It’s awesome to have the ability to choose between so many different frameworks, however there are differences in terms of performance, API and licensing. I’ll take a highlevel peek at couple of them in terms of their API and how to use them in your project. I’ll only highlight and compare the JSON consuming part of the API, although serialization works similarly.

All of the frameworks presented here, are released under a license which allows you to use them in closed-source projects, so no problem here. Please keep in mind, that I don’t factor in performance. If you are interested there are lots of comparisons out there.

The first framework I’d like to highlight is YAJL (Yet Another JSON Library). I used YAJL in a couple of projects and I really like its approach. It’s available for Mac and iOS. In order to use it in your project, you can either include it as a static library or add the source code to your Xcode project. In the latter case, you’ll need to add other linker flags in your target in order to use YAIL.

From an API point of view, YAIL comes with a bunch of categories on NSString or NSData, helping you to transform the JSON string into various Objective-C data types. It looks something like this:

#import <YAJLiOS/YAJL.h>
NSString *JSONString = @"[1, 2, 3]";
NSArray *arrayFromString = [JSONString yajl_JSON];
gist

I really like YAJL’s transparent approach using categories, although using it as a static library and configuring linker flags can be a source of error.

The next framework I’d like to talk about is JSONKit. I’m using it on my current project at work and so far I’m quite happy. Just like YAJL, JSONKit comes with categories on NSString and NSData. In addition to that, it has an explicit parsing interface to create objects from those two data types.

#import "JSONKit.h"
NSString *JSONString = @"[1, 2, 3]";
NSArray *arrayFromString = [JSONString objectFromJSONString];
gist

I like JSONKit, especially because it manages to provide an easy to use API. It also doesn’t hurt, that it seems to be quite fast.

At work we use json-framework a lot. It doesn’t stand out much from the frameworks discussed before. The only real difference is, it has been around for quite a while. Just like JSONKit it provides an explicit interface or categories to parse JSON from NSString and NSData. Let’s look at the same example of the previously discussed frameworks:

#import "JSON.h"
NSString *JSONString = @"[1, 2, 3]";
NSArray *arrayFromString = [JSONString JSONValue];
gist

A nice feature of json-framework’s explicit parser interface is the configuration of parsing depth. It could be useful, if you have to parse large JSON structures and only need the top level objects.

The last framework I want to peek at is TouchJSON. As with json-framework, I’ve never used this one in production myself. The only way to get an overview is by looking at a couple of samples and running them in an Xcode project. In order to use TouchJSON, you simply have to include the source in your project. The sample code used to previously illustrate how to use the framework looks a little different:

#import "CJSONDeserializer.h"
NSString *JSONString = @"[1, 2, 3]";
NSData *jsonData = [JSONString dataUsingEncoding:NSUTF8StringEncoding];
NSError *error = nil;
NSArray *array = [[CJSONDeserializer deserializer] deserializeAsArray:jsonData 
                error:&error];
gist

In contrast to all other frameworks, TouchJSON only supports a category to create a NSDictionary. Everything else needs to be done using the explicit interface CJSONDeserializer.

As you can see, almost all API’s are used the same way. They all provide categories, except for TouchJSON. You can simply use iOS’ datatypes to retrieve native datastructures from JSON. I like categories, especially when used in such a way as the JSON frameworks presented here. It just feels natural to call objectFromJSONString on a NSString containing JSON.

There is no clear winner, but I think JSONKit would be my framework of choice, if I had to pick. I love the transparent, intuitive approach of its API and for me that’s more important than other aspects of a framework.


// other posts