Ccna final exam - java, php, javascript, ios, cshap all in one. This is a collaboratively edited question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
Thursday, May 31, 2012
Best JSON library to use when developing an iPhone application?
There are a few JSON libraries/frameworks available for Objective-C developers, but I wanted to get the opinion of the resident gurus here on which one is the best, and why.
I think "best" depends on what features you're interested in. If you are after a JSON parser/generator that strictly follows the JSON protocol, then you could do worse than use json-framework. (Disclaimer: I am its author.)
It also has a features such as protection against deeply nested structures (could break the stack if left to run wild), pretty-printing the JSON output and sorting the dictionary keys. (This is useful if you want/need to make sure the key ordering in the output is the same after adding/removing entries to a dictionary. Good for automated tests, for example.)
There is on-line documentation generated from the source available.
I've been using touchJSON for a while, but noticed that it has a problem with line breaks (at least from a UITextView on iOS). Thought about trying to fix it, but bailed out since the character replacement code in TouchJSON tries to be too smart for its own good.
Switched to json-framework. Took 10 minutes, and it handles line breaks correctly. My vote is for json-framework.
I've used both SBJson (mentioned above) and YAJL in projects. SBJSON is a lean and easy to use parser, I've found it easy to integrate but it's performance does fall short when compared to YAJL.
The YAJL implementation comes with pretty effective type coercion (integers, floats, bools, etc turn in to NSNumbers), speed and event driven (sax style) parsing model. The event driven parsing has been a big win when dealing with larger data sets.
I have actually just finished developing a very fast JsonSerializer (benchmarks here) which also supports Mono (The main reason why I wrote it). It doesn't use any Reflection.Emit so there is a good chance it will just run in MonoTouch. If I get the time, I plan to verify that it works in MonoTouch next week.
Basic Example
var customer = new Customer { Name="Joe Bloggs", Age=31 }; var json = JsonSerializer.SerializeToString(customer); var fromJson = JsonSerializer.DeserializeFromString<Customer>(json);
In the meantime you can check out these live examples which are hosted on CentOS/Nginx/Mono FastCGI.
EDIT: BTW I have now verified this and it does in-fact work with MonoTouch.
I used the JSON Framework, it appeared to work alright. However at some point I was getting alloc error for SBJsonParser after I had some HTTP communication.
Luckily I found JsonKit and my problems have been solved. Its lean and easy to use. In the end what do we need more than the ability to transform some array or dictionary in a JSON string to call your web service and to transform the webservice response from JSON notation back into an array or a dictionary!?
Touch JSON tends to be the best in terms of speed and unit test coverage. It's also the most widely adopted and actively developed.
ReplyDeleteAnother ObjC JSON library is JSONKit. It tends to be faster and use less memory than the (already mentioned) alternatives. BSD License.
ReplyDeleteJSONKit: https://github.com/johnezang/JSONKit
I've had a great experience with http://github.com/stig/json-framework the API is simple and effective.
ReplyDeleteI think "best" depends on what features you're interested in. If you are after a JSON parser/generator that strictly follows the JSON protocol, then you could do worse than use json-framework. (Disclaimer: I am its author.)
ReplyDeleteIt also has a features such as protection against deeply nested structures (could break the stack if left to run wild), pretty-printing the JSON output and sorting the dictionary keys. (This is useful if you want/need to make sure the key ordering in the output is the same after adding/removing entries to a dictionary. Good for automated tests, for example.)
There is on-line documentation generated from the source available.
There is a comprehensive review by cocoanetics.com. JSONKit is likely to be the winner.
ReplyDeleteJust to clarify for future readers, json-framework and SB-JSON is the same thing.
ReplyDeletePlease correct me if wrong.
I've been using touchJSON for a while, but noticed that it has a problem with line breaks (at least from a UITextView on iOS). Thought about trying to fix it, but bailed out since the character replacement code in TouchJSON tries to be too smart for its own good.
ReplyDeleteSwitched to json-framework. Took 10 minutes, and it handles line breaks correctly. My vote is for json-framework.
I have been using the json-framework from on google code. It has worked very well for me.
ReplyDeleteI've used both SBJson (mentioned above) and YAJL in projects. SBJSON is a lean and easy to use parser, I've found it easy to integrate but it's performance does fall short when compared to YAJL.
ReplyDeleteThe YAJL implementation comes with pretty effective type coercion (integers, floats, bools, etc turn in to NSNumbers), speed and event driven (sax style) parsing model. The event driven parsing has been a big win when dealing with larger data sets.
SBJSON:
http://code.google.com/p/json-framework/
YAJL:
http://lloyd.github.com/yajl/
I have actually just finished developing a very fast JsonSerializer (benchmarks here) which also supports Mono (The main reason why I wrote it). It doesn't use any Reflection.Emit so there is a good chance it will just run in MonoTouch. If I get the time, I plan to verify that it works in MonoTouch next week.
ReplyDeleteBasic Example
var customer = new Customer { Name="Joe Bloggs", Age=31 };
var json = JsonSerializer.SerializeToString(customer);
var fromJson = JsonSerializer.DeserializeFromString<Customer>(json);
In the meantime you can check out these live examples which are hosted on CentOS/Nginx/Mono FastCGI.
EDIT:
BTW I have now verified this and it does in-fact work with MonoTouch.
I used the JSON Framework, it appeared to work alright. However at some point I was getting alloc error for SBJsonParser after I had some HTTP communication.
ReplyDeleteLuckily I found JsonKit and my problems have been solved. Its lean and easy to use. In the end what do we need more than the ability to transform some array or dictionary in a JSON string to call your web service and to transform the webservice response from JSON notation back into an array or a dictionary!?