Pages

Thursday, March 24, 2011

JSON Vs XML


An article without an introduction is like hitting on the head. It gets all fuzzy. JSON stands for JavaScript Object Notation which is a built in feature of JavaScript. It is based out of ECMA standards. XML stands for Extended Markup Language. It is W3C supported standard. JSON and XML are common data interchange format. They are mainly used to send/receive data between applications running on same or different platforms. They are well supported by open source libraries for almost all programming languages like C, C++, Java, ASP, Ruby, VB, Python, PHP, Perl, Adobe Cold Fusion. They are handy and important for mobile application developments as well. Apple's Objective C, Google's Android, RIM's Blackberry, Symbian OS they all support both JSON and XML.

Though both JSON and XML are fairly easy to understand, programmers choose JSON for its simplicity. JSON gives a Deja Vu to programmers, reminding them of their first love, The powerful C Language. The data structure format of JSON is similar to C. Web programmers prefer XML for its HTML like structure. Let's assume, your requirement is to send fire to the sun or to receive darkness from your shadow. How would you choose between JSON and XML? Someone, somewhere makes a bold move everyday. It's your turn, boss. So, What differentiates JSON and XML?

From the data exchange perspective, JSON is light weight when compared to XML. JSON is beautiful without a makeup. JSON carries no unwanted tags like namespace, unicode, internationalizations and it represents the object as it is. This makes serialization/deserialization of an object to JSON  faster than XML. This is a crucial point for Mobile Applications. Extra data transfer means extra processing, which means extra CPU, which means extra battery usage, which means unhappy users. JSON libraries are available for conversion between JSON to Java, JSON to CSV, JSON to HttpHeaders, JSON to Cookies, JSON to XML etc. GSON (Google JSON) library helps converting data between JSON and Java. It is as easy as you get a Grinder or Mixie or a TV. This conversion needs no schema. Gson's toJson(), fromJson() methods does all the conversions. It creates only one java object or JSON string. GSON library can be used on .class files when the source java file is not available (assuming the class does not have an inner class). Customized representation of objects are possible with GSON. JSON needs special attention when the object contains generics like List of objects, Maps etc.

XML is everywhere. The kingdom of XML is much more bigger than JSON. Most of the enterprise applications serve their business as Webservices (Soap Based) where XML is widely used. With RESTFul based webservices, JSON is slowly gaining popularity. Reading data from a stream or downloading a file is lot easier with XML than JSON. XML can handle data streaming with SOAP based webservices. The binary content of an entire file can be encoded as a base64 string and embedded in the XML message (for bigger files, use HTTP or FTP and don't use web services). RSS, Atom, XHTML, WSDL are all based out of XML. Apart from data interchange, XML is also used for configurations, settings replacing the traditional properties files. Spring Framework's IOC, JSF's faces-config, Strut's struts-config, Web Server's deployment descriptor, Hibernate's HBM files, Ibatis's SQL maps are all based out of XML. With such wide usage and flexibility, XML has become developer's reliable bet and is blindly used in most of the applications for data interchange as well. JAXB (Java API for XML Binding) library helps convering data between XML and Java. This conversion needs the XML schema to be known.  No conventional parsing is required. The 'marshall' / 'unmarshall' method call does all the conversion for you both ways. Understanding a Genius (JAXB) is a complex thing. I guess, you agree.

An article without a conclusion is like ending a movie at the interval. The audiences don't get a damn. With technologies evolving wisely, it becomes important to chose them wisely too. While designing enterprise applications, consider JSON to support faster data transfer, which means a lot for tablets, netbooks and mobile devices. At the same time, consider XML because most of your voters are still using the XML band and looking out for data to be transferred magically. If the amount of data to be transferred is big or for fairly complex objects, choose XML. You would not want to lose your customers. So, without rationalizations, without regrets, provide support for both JSON and XML. While developing applications for handsets like mobiles, consider JSON. Your users won't care about the beauty inside but will still be happy with your application's performance.