Andrew
Andrew

Reputation: 238667

Which is faster/easier to work with in an iPhone app: XML or JSON?

I am starting to create an iPhone application that will interact with a public API.

My question is, which will be faster and/or easier to work with: XML or JSON?

Upvotes: 3

Views: 1006

Answers (5)

Brad Larson
Brad Larson

Reputation: 170309

According to Sam Soffes, TouchJSON outperforms XML property lists:

When I was preparing for my talk I decided to do some benchmarks to show how much more awesome plists were than JSON. The plist version was about 8 times faster than my JSON Framework version. I was pretty happy with that result. My friend, Jake, said he was using TouchJSON to parse JSON in his apps, so I figured I’d go ahead and benchmark that one too. I was expecting JSON Framework to beat it because the interface to the JSON Framework is a lot simpler than the interface to TouchJSON.

What I found was very surprising. TouchJSON actually beat plists. It was slightly faster in every test I ran. This is awesome because plists have a much larger file size. They are usually about twice as big as JSON files due to all of the extra markup.

However, that is a single data point.

Upvotes: 5

Paul Creasey
Paul Creasey

Reputation: 28824

JSON is quite a lot terser which saves bandwidth and makes processing more efficient, so i'd choose that.

Upvotes: 2

Adrian Kosmaczewski
Adrian Kosmaczewski

Reputation: 7956

I would also use JSON, with TouchJSON (as suggested earlier). The payload is smaller and the parsing is faster in my own tests. Oh, and by the way, don't use NSXMLParser, it's really slow. Use libxml2 or TouchXML instead. Check out this article about a direct experience with JSON on the iPhone:

Another possible approach is to generate XML or binary Property Lists, and then deserializing that into native objects. Your server-side application might generate the plist format for you using plistlib on Python or plist for Ruby.

Upvotes: 1

Mike
Mike

Reputation: 24954

In terms of built-in APIs, to my knowledge, there's no native support for JSON, so XML would be the only choice if you didn't want to use external libraries.

That said, JSON tends to be much easier to work with because it synthesizes directly into Cocoa objects(i.e. NSDictionary, NSArray, NSString, NSNumber), so I would say JSON is much easier to work with if you're willing to use an external library, and you have good server-side support for JSON. I've had good luck with the json-framework library, so my advice is to give that a try.

Another bonus of JSON is that it typically(almost always) will produce smaller file transfer sizes over the network because of its formatting. While saving a few kb might be overlooked, it is a major win on a mobile device with limited bandwidth while on a cell network.

Upvotes: 5

duffymo
duffymo

Reputation: 308743

Can't tell with so little information. It depends on the quality and capabilities of the libraries on either end that deal with that data form.

But you already know that JSON will be more compact, because it doesn't have to deal with closing tags (variables and open tags seem a wash to me). Fewer bytes on the wire for JSON.

Upvotes: 2

Related Questions