views:

4615

answers:

6

I'm developing a new application for the company. The application have to exchange data from and to iPhone.

Company server side uses .NET framework.

For example: the class "Customer" (Name, Address etc..) for a specific CustomerNumber should be first downloaded from server to iphone, stored locally and then uploaded back to apply changes (and make them available to other people). Concurrency should not be a problem (at least at this time...)

In any case I have to develop both the server side (webservice or whatever) and the iPhone app.

I'm free to identify the best way to do that (this is the application "number ONE" so it will become the "standard" for the future).

SO, what do you suggest me ?

Use SOAP web services (XML parsing etc..) or user JSON ? (it seems lighter...) Is it clear to me how to "upload" data using SOAP (very long to code the xml soap envelope ... I would avoid) but how can I do the same using JSON ?

The application needs to use date values (for example: last_visit_date etc..) what about date in Json ?

Your advices or links will be precious to me

Thanks

Dario

+7  A: 

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.

Its also easier to use in javascript code as you can simply pass the data packet directly into a javascript array without any parsing, extracting and converting, so it is much less CPU intensive too.

To code with it, instead of an XML library, you will want a JSON library. Dates are handled as you would with XML - encode them to a standard, then let the library recognise them. (eg here's a library with a sample with dates in it)

Here's a primer.

gbjbaanb
"Its also easier to use in javascript code as you can simply pass the data packet directly into a javascript array without any parsing, extracting and converting, so it is much less CPU intensive too."Do you mean using eval? If you do it safely, which eval is not, then you (or the library) actually does have to do a little bit of that with JSON.
GreenieMeanie
Could you backup the considerable difference with less data over the network by providing some numbers and/or links? I'm sure it is the case, but I ask myself how much dirrence it is in a real life app
Tim Büthe
it varies depending on your network connection. If you have a 100Mbps fibre link, you won't notice. If you're streaming a choppy 2kbps mobile link, you certainly will. Besides, there's no reason to be inefficient if something else will do the same job better.
gbjbaanb
+6  A: 

Ah, the big question: JSON or XML?

In general, I would prefer XML only when I need to pass around a lot of text, since XML excels at wrapping and marking up text.

When passing around small data objects, where the only strings are small (ids, dates, etc.), I would tend to use JSON, as it is smaller, easier to parse, and more readable.

Also, note that even if you choose XML, that does not by any means mean you need to use SOAP. SOAP is a very heavy-weight protocol, designed for interoperability between partners. As you control both the client and server here, it doesn't necessarily make sense.

Avi
+1 for "even if you choose XML, that does not by any means mean you need to use SOAP"
Tim Büthe
+3  A: 

Consider how you'd be consuming the results on the iPhone. What mechansim would you use to read the web service response? NSXMLParser?

How you consume the data would have the biggest impact on how your serve it.

Are JSON and SOAP your only options? What about RESTful services?

Take a look at some big players on the web that have public APIs that are accessible by iPhone clients:

Twitter API FriendFeed API

Also, review the following related articles:

How to parse nested JSON on iPhone RESTful WCF service that can still use SOAP Performance of REST vs SOAP

JBRWilkinson
+2  A: 

JSON has following advantages:

  1. it can encode boolean and numeric values ... in XML everything is a string
  2. it has much clearer semantics ... in json you have {"key":"someValue"}, in XML you can have <data><key>someValue</key></data> or <data key="someValue" /> ... any XML node must have a name ... this does not always make sense ... and children may either represent properties of an object, or children, which when occuring multiple times actually represent an array ... to really understand the object structure of an XML, you need the scheme ... in JSON, you need the JSON only ...
  3. smaller and thus uses less bandwidth and memory during parsing/generation ...

apart from that, i see NO difference between XML and JSON ... i mean, this is so interchangable ... you can use JSON to capture the semantics of SOAP, if you want to ... it's just that SOAP is so bloated ... if you do want to use SOAP, use a library and generators for that ... it's neither fun nor interesting to build it all by hand ...

using XML RPC or JSON RPC should work faster ... it is more lightweight, and you use JSON or XML at will ... but when creating client<->server apps, a very important thing in my eyes, is to abstract the transport layer on both sides ... your whole business logic etc. should in no way depend on more than a tiny interface, when it comes to communication, and then you can plug in protocols into your app, as needed ...

back2dos
'in XML everything is a string' - not true. http://en.wikipedia.org/wiki/XML_Schema_(W3C)#Data_types
tomfanning
@tomfanning: I really know **few** people using XML Schema. Even DTD is really something I see rarely. But yes, you can make XML a type safe by throwing XSD at it, which makes it even more cumbersome and overcomplicated.
back2dos
Not true. In the Java World for example, it is very common to write the XSD and generate classes from that. You can then parse and serialize from these Objects to XML and back. check out http://xmlbeans.apache.org/ for example.
Tim Büthe
A: 

There are more options than just SOAP vs JSON. You can do a REST-based protocol (http://en.wikipedia.org/wiki/Representational_State_Transfer) using XML. I think it's easier use than SOAP and you get a much nicer XSD (that you design.) It's rather easy for almost any client to access such services.

On the other hand, JSON parsers are available for almost any language and make it really easy to call from JavaScript if you'll use them via AJAX.

However, SOAP can be rather powerful with tons of standardized extensions that support enterprise features.

Norman
+1  A: 

You could also use Hessian using HessianKit on the iPhone side, and HessianC# on the server side.

The big bonuses are: 1. Hessian in a binary serialization protocol, so smaller data payloads, good for 3G and GSM. 2. You do not need to worry about format in either end, transport is automated with proxies.

So on the server side you just define an C# interface, such as:

public interface IFruitService {
  int FruitCount();
  string GetFruit(int index);
}

Then you just subclass CHessianHandler and implement the IFruitService, and your web service is done.

On the iPhone just write the corresponding Objective-C protocol:

@protocol IFruitService
-(int)FruitCount;
-(NSString*)GetFruit:(int)index;
@end

That can then be access by proxy by a single line of code:

id<IFruitService> fruitService = [CWHessianConnection proxyWithURL:serviceURL 
                                                          protocol:@protocol(IFruitService)];

Links:

HessianKit : http://sourceforge.net/projects/hessiankit/

PeyloW