Formats for communicating between backend and Objective-C/Cocoa - iphone

I'm developing an iPhone app that is connected to a backend server. It needs to communicate with it many times, through several requests. I'm sending HTTP messages, but I want to receive more complex responses that I can parse somehow. Supposedly, I can provide any type of format for responses from the server, so my question is: which one would be easier(maybe even faster) to use/parse for Objective-C/Cocoa, and transform in a dictionary of some kind?
I know it's a bit subjective but I still think it's a valid question, some programming languages just have more support for some formats and less for others.

From Cocoa 's perspective, the simplest format is a property list as Cocoa can parse this natively into a dictionary or an array.
You can use NSDictionary's +dictionaryWithContentsOfFile: and +dictionaryWithContentsOfUrl: to read a plist file into a dictionary.
If your plist data is not in a file, you can also convert an NSData object containing plist data to a dictionary with +[NSPropertyListSerialization dataFromPropertyList:format:errorDescription:] or convert an NSString to a dictionary with -[NSString propertyList].

PList is a good answer and very usable, but many server side people will be more comfortable producing JSON - TouchJSON is a very good JSON parser for the iPhone.

While there is a plist gem for ruby, JSON or (raw) XML are much more popular outside the Apple world. For instance most JavaScript libraries are set up to speak one or both of these.
So if you're exclusively talking to an iPhone, the plist is probably a good choice, but otherwise you should consider using JSON (or XML).

Related

iPhone : Which type of parser recommended for data parsing between iPhone and web-service?

Can any body tell me which parser is best in my condition? XML, JSON or any else ?
The app contains feature link sync the data, communicate with the web-service and etc. So I am targeting parser which is light weight and fast.
I have lots of data which are parsed between iPhone and server.
Which are the option? Or any good link for comparison between different parser ?
I would suggest JSON as in terms of data size json tends to be compact compared to XML. Which will reduce time spent in network transfer (for your data).
You could use any available JSON parsers to directly get ObjectiveC objects. Some Json parsers -
NSJSONSerialization
json framework
jsonKit
I personally prefer jsonKit as its supposed to be fastest of all.
Of course it's JSON.
This is the frequent question asked on stack.You could find much better answer just by Goggling.
JSON has several advantages over XML. Its a lot smaller and less bloated, so you will be passing much less data over the network - which in the case of a mobile device will make a considerable difference.
Refer to JSON Tutorial for iPhone.
Mr.Devang. I recommend NSXMLParser. If your data or response from service will be in XML format please use NSXMLParser. Apple have inbuilt xml parsing tool. Please refere Apple document fir NSXMLParser,
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSXMLParser_Class/Reference/Reference.html
http://developer.apple.com/library/mac/#documentation/cocoa/reference/NSXMLParserDelegate_Protocol/Reference/Reference.html
And also, if you receive your web-service response in JSON format, you can use SBJSON to parse the JSON files. You can get the json framework form this link,
https://github.com/stig/json-framework/
Thanks.
If you would like to know about different XML Parsers available and comparison between them, you can visit this tutorial which shows different xml parsers available and comparison between them.

Parsing large quantities of JSON data in iOS

I'm getting a large JSON string (11MB) from a web service. When I parse the data using JSONKit, my app reaches 70MB, I get memory warnings, and the app crashes.
How can I parse this data?
the easiest solution is reducing the json size you are getting from the server. If you cant to it, the only way to parse huge JSON is using lazy evaluation.
I dont think there is a JSON lib for objective-c that supports lazy evaluation. however you can implement one.
Your best bet is to look at the YAJL JSON parser, that supports event driven parsing. Then you can parse the JSON as it comes down in a data feed, and not have to store the whole thing in memory at once.
https://github.com/gabriel/yajl-objc
Sorry, I don't know of any code examples that demonstrate this use in practice.
SBJson supports parsing a stream of data. This lets your process your document bit by bit so you don't need to hold on to the entire document. The distribution contains two examples of how to use this. First there's the StreamParserIntegrationTest.m and next there's the TweetStream demo app: a twitter application that will sit and parse a HTTP stream all day (if you let it) displaying each tweet as they come in and then throw them away.
(Disclaimer: I am SBJson's author.)
11 mb is a largedata and solution is only to minimise your size of data
JSON support is not native to iOS, but there is a great framework for this exact purpose: http://code.google.com/p/json-framework/
This framework supports conversion from raw JSON objects to Objective-C objects (NSArray, NSDictionary) and vice versa.

Saving and Editing JSON on iPhone/iPod

What's the best method for editing and saving a JSON file on the iPhone/iPod? I know there are libraries that allow you to easily read JSON data, but are there any that allow you to generate it?
TouchJSON reads and generates JSON, is quite fast, and uses very little memory.
JSON Framework is supposedly faster, but uses much more memory.
The iPhone also includes a built-in JSON parser/generator, but it's only available through private APIs. You probably shouldn't use this one.
TouchJSON allows conversion both to and from JSON with a single call like:
[[CJSONSerializer serializer] serializeObject:someDictionaryofYours];

Storing my ObJ-C class online. (amazon web services?)

Okay this is a fairly broad question. This is my first App and I'm not sure the best way to go about this. The app is on the IPHONE.
I have a 'Restaurant' class. The restaurant has many different attributes and opening times.
I currently store a restaurant in an instance of nsdata (it complies to NSCoding) LOcal storage is easy and I just use nsdata.
I have just built in an import/export function. I want these two methods to "post" a restaurant to the web and "get" a restaurant from the web. I know I can do this with NSUrlConnection and I have it up and working.
However I want to be cleverer about it. For instance what if I want to take my online list of restaurants that have been submitted and build a web interface that can also interact with the data?
This is what I am thinking so far:
Create a 'toString' method that will convert my class into a textual representation. Then store the string on a server. This will replace storing nsdata online. Does this sound appropriate?
However, I want to be able to query the amount of restaurants and have a bit more control over my online 'database'. Is XML the answer? I was about the start looking into Amazon Web Services and learn either Json or Rest.
Any bits of advice?
Thanks
Dan
I am not very familiar with iPhone development yet, but if there are reasonably easy way to consume web services I would recommend using that with typed parameters. This would make these services more "usable" from other systems too, they would not need to know how to serialize/pack objects to a string but only how to call a function: storeRestaurang(string name, int rating...) etc.
This is an article I will check out myself since I hope to get into iPhone development a bit more: http://icodeblog.com/2008/11/03/iphone-programming-tutorial-intro-to-soap-web-services/
Looks like you want to use a plist.
Any of the cocoa collection classes can be written out to a plist, which is a breed of xml file. The sweet part is if you use standard cocoa value classes, you can write the collection out, and read it back in later.
NSString, NSNumber, NSDate, NSData, NSAray, NSDictionary objects can be put into a collection and stored to a plist directly. Other classes will have to be serialized into NSData then written to disk as a plist. For truly custom data, an NSKeyedArchiver is probably what you want.
For simple strings and numbers data, pack it all into an NSDictionary and then write it to a plist. Now look around on your mac and you'll see just how popular plists are.
NSDictionary API - Storing Dictionaries
Property List Programming Guide
Archives and Serializations Programming Guide for Cocoa
Learn about REST, it's the easiest and cleanest way to provide a web-based API. Some frameworks, like Ruby on Rails, give you a REST interface right out of the box.
Then, you'll need to find a way to convert your objects on the iPhone into a REST-friendly format. XML and JSON are both options, but unfortunately I haven't seen any code for converting to/from XML or JSON that I can really recommend, since I haven't tried any just yet. But, there are some projects that might lead you in the right direction. One of the most promising-looking is this:
http://github.com/yfactorial/objectiveresource/tree/master

Is there an easy way to work with web services for iPhone dev?

We want to take XML data and convert it to an NSDictionary object, but we don't want to manually iterate over the XML. Is there an easy way to do this? How are you doing web services for your iPhone app?
If you have control over the XML output you could try creating a property list which you can then read into a dictionary using -dictionaryWithContentsOfURL: (though the better asynchronous way would be to get the data using an NSURLConnection and then converting the data using the -propertyList method on NSString). You can find more about property lists here: http://developer.apple.com/documentation/Cocoa/Conceptual/PropertyLists/Introduction/chapter_1_section_1.html
Of course the best solution is to use a RESTful client and use a combination of NSURLConnection to get/send the data and the TouchXML classes (http://code.google.com/p/touchcode/wiki/TouchXML) to parse the data, though this would require more work to put the data into a dictionary. Of course if these are going to be the main data objects in your system you really want to be using either a custom class or SQLite to store the data as it provides you much more reliability in testing your app than a dictionary.
If you can control the server output, try using plists. Otherwise you're stuck with parsing XML (or JSON if the server can do that), but there are frameworks you can use. See the answer to this question.
Also, here is a good overview of how to do RESTful clients on the iphone:
https://developer.apple.com/webapps/articles/creatingrestfulclients.html
You can return the data in JSON format. There are many open-source JSON parsers available for the iPhone (TouchJSON being one).
There's another class available called NSPropertyListSerialization which gets you a dictionary from data.
You can do something like this with the data you receive
NSDictionary* propertyList;
NSPropertyListFormat format;
NSString *errorStr;
propertyList = [NSPropertyListSerialization
propertyListFromData:receivedData
mutabilityOption: NSPropertyListImmutable
format: &format
errorDescription: &errorStr];
Sorry, don't know what tags are used here for formatting code!