Cocoa JSON parsing libraries, part 2
A few months ago I wrote a post about JSON parsing libraries for Cocoa. I compared 4 libraries – BSJSONAdditions, JSON Framework, TouchJSON, and YAJL, I ran a benchmark on all of them, and the conclusion was that YAJL was the fastest and BSJSONAdditions was way slower than the rest.
Last week John Engelhart commented on that post, mentioning his own JSON library JSONKit, claiming that it’s really fast. Of course I had to check if that was true :)
I couldn’t just run the same test now on latest JSONKit and compare the result against the old results, that wouldn’t be very fair – so I checked the repositories of the other projects, downloaded latest versions and rerun all the tests.
Here are the results (I took a median value from 5 runs for every library):
Simulator
Framework | time [s] |
---|---|
BSJSONAdditions | 0.184697 |
TouchJSON | 0.037910 |
YAJL | 0.016679 |
JSON Framework | 0.015583 |
JSONKit | 0.012852 |
Device
Framework | time [s] |
---|---|
BSJSONAdditions | 10.526504 |
TouchJSON | 2.323277 |
YAJL | 1.021358 |
JSON Framework | 0.907641 |
JSONKit | 0.587229 |
Conclusions:
- For BSJSONAdditions, TouchJSON and YAJL the results are very similar to what I got last time.
- BSJSONAdditions, which I used before and to which I even contributed some patches, is still slow as hell. The project was last updated in 2009, which means it’s basically dead. Don’t use it.
- TouchJSON and YAJL are still maintained (TouchJSON has moved from Google Code to GitHub); however, that didn’t make them any faster than before.
- JSON Framework has also moved to GitHub, and it seems the author has made a lot of progress on it: it’s now more or less 3× faster than before, which makes it slightly faster than YAJL, the previous winner.
- But the biggest surprise was JSONKit – it seems John was right, it really is super fast. It’s about 50% faster than YAJL and JSON Framework, and 20× faster than BSJSONAdditions. It’s also the easiest to install – it’s just two files, no frameworks, no subdirectories, no need to change anything in Xcode project settings.
So if you’re looking for a fast JSON library for your iPhone app, choose JSONKit.
Again, the test project is here (430 KB).
Update: John Engelhart told me that my JSON is a kind of worst case scenario, in that it contains mostly randomized data. In real data there should be much more repeating keys and values, which can be cached by JSONKit, and in such cases it should be even faster than here. Go figure…
7 comments:
Evan Doll
For what it's worth, I came across this post when looking for faster JSON parsing in my project and, based on the stellar numbers & good past experience with RegexKitLite, decided to give JSONKit a try.
It is indeed as fast as advertised, but I ran into overrelease crashers with some data sets (for example, empty arrays). The GitHub repository hasn't been updated in about four months & the code is relatively obtuse to debug myself.
This doesn't give me confidence that it's suitable for production quality software right now. I'll be sticking with JSON Framework in my project.
johne
Well, if you don't file a bug, it's actually kind of hard to fix them.
What feedback I have gotten from people who are using it in their products is that not only is it fast, one of the reasons they were switching to JSONKit was due to bugs and other problems in the other libraries. I've kept in touch with a few of them and asked "Any problems so far?" and they've said "Amazingly, no."
So the reason why it hasn't been updated is because so far, no one has filed any bugs, and those that are using it and I've kept in touch with haven't reported any problems.
But with all due respect, if you didn't file a bug report, it's pretty hard to take your complaint seriously.
Roy
Been using JSONKit for a few days now and haven't had any issues at all, and that includes deserialisation AND serialisation of objects.
Thanks for the JSON lesson.
Evan Doll
Ignore my original comment above. JSONKit is a total game-changer. I highly recommend it.
Ashish
hi ,i am using RESTkit webservices,and i have to create newsfeed. so i have to get json parsing and i am stuck somewhere.
restapi.m
-(void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response {
else if ([request isGET]) {
NSLog(@"GET response: %@", [response bodyAsString]);
if ([request.resourcePath isEqualToString:[TeamieRestAPI resourceURL:TeamieUserNewsfeedRequest]]) { // if it's a request to get User Newsfeed
if ([response isOK]) {
NSDictionary * newsfeed = [[response bodyAsString] objectFromJSONString];
[self.delegate requestSuccessful:TeamieUserNewsfeedRequest withResponse:newsfeed];
}
else {
[self.delegate requestFailed:[NSString stringWithFormat:@"%i", response.statusCode] forRequest:TeamieUserNewsfeedRequest];
}
}
-(BOOL)teamieUserNewsfeed:(NSDictionary*)params{
NSString * newsfeedURL = [TeamieRestAPI resourceURL:TeamieUserNewsfeedRequest];
[[RKClient sharedClient] get:newsfeedURL queryParams:params delegate:self];
#ifdef DEBUG
NSLog(@"Newsfeed request sent...");
#endif
return YES;
}
in newsfeedviewcontroller.m
// the function that is called once the Newsfeed is loaded
// Also contains code to parse throught the JSON structure
-(void)userNewsfeedDidLoad:(NSDictionary *)response {
NSDictionary * newsfeed = [response valueForKey:@"thoughts"];
// print the 'title' and 'method' of all filters
for (id item in [newsfeed valueForKey:@"filters"]) {
NSLog(@"Filter Title: %@ with method: %@", [item valueForKey:@"title"], [item valueForKey:@"method"]);
}
// print 'html' of each thought along with 'timestamp' and 'real_name' of the user who posted the thought
for (id thoughtid in [newsfeed valueForKey:@"thought"]) {
NSDictionary * thought = [[newsfeed valueForKey:@"thought"] valueForKey:thoughtid];
NSLog(@"Message '%@' %@ by %@", [[thought valueForKey:@"message"] valueForKey:@"html"], [[[thought valueForKey:@"timestamp"] valueForKey:@"time"] valueForKey:@"text"], [[thought valueForKey:@"user"] valueForKey:@"real_name"]);
}
}
i want to show on uitable view -- html as message ,, text as time ,, real_name as name
i am getting some problem parsing these elements and shwing on tableview..
can you help in this?
Spencer
As far as YAJL, were you testing the C interface, or the ObjC wrapper that parses into NSCollection objects? I'm wondering if the speed gap holds up if you're using YAJL's streaming C interface to build custom objects directly rather than building a bunch of collections first.
Kuba
@Spencer: I've tested the ObjC interface (https://github.com/gabriel/yajl-objc), I have no idea how much faster it would be if I used the C API directly, but I generally try to avoid any C APIs unless there's really no other way :)