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.

Saturday, December 18, 2010

HTML5 Editors

HTML5
When something new comes along, the old has to give away. Do you believe so? HTML5 is evolving and is already well supported by most of the smart phones and leading browsers. Ipad/IPhone enabled websites are all powered with HTML5. With increasing usage of Smart Phones, there is a compulsory need for every website to have a mobile friendly version. With the promising features like Offline support, enhanced multimedia, graphics, interactive Drag & Drop and advanced UI forms, HTML5 will eventually become the standard of the web.



Editors for HTML5:
The specification for this major version of HTML is not finalized yet. Probably because of this reason, there are not much HTML5 editors available for developers. Till HTML4, W3C would finalize the specification first and then the browsers and editors would start to implement them. With Google and Apple heavily backing up HTML5, browsers have started implementing the draft version of HTML5 and kept moving forward on their browse race. Developers are left with only a few options to choose their favorite editor for HTML5. The editors available on markets like Dreamweaver CS5, Aloha Editor (WYSIWYG based) are licensed and heavily priced. Web developers are forced to use Text Editors like Textpad, Editplus, Notepad++ or Eclipse IDE without any additional help for quick reference.



Eclipse & HTML5:
With the support of third party plug-in like Aptana Studio 3.0 beta, Eclipse (Version 3.4 or above) is able to provide HTML5 support for developers to some extent. The good thing about this plug-in is that, it supports code assistant for JQuery (Version 1.4.2). For web developers, this is a great boon, I would bet. The code assistant tool recognizes HTML4 tags well but eclipse will flag a compilation error for new HTML5 tags. Even the latest version of Eclipse 3.6 does not have a built in support of HTML5. Aptana has little support for CSS3 and built in Preview option for HTML files.. To install the plugin,


Netbeans & HTML5:
Netbeans.org has recently released their Beta version of IDE 7.0 with HTML5, Javascript, CSS, AJAX, JSON and JavaFX formatting support. The interesting feature about this release is that the code assistant tool is able to identify most of the HTML5 tags supported by leading browsers like Chrome, Firefox, Opera and Safari. The code assistant tool briefs the HTML5 tag syntax, attributes and the browsers that currently support the tag. It also shows example code with appropriate syntax and suggestions. The code assistant works out well for Javascript and CSS files as well but there is no support for JQuery. It also has an option to view the HTML5 specification (draft specification as of September 2010) in a separate browser. There is no need to update or install plug-ins for HTML5 support on it.












It would be great if we have the power of Aptana on Netbeans. But they themselves are competitors on Ruby and PHP front. We have to wait for Aptana Studio to fully support HTML5 or Netbeans to support JQuery. When something new comes along, it doesn’t mean that the old has to fail. They can live in harmony with one another and generate greater results. Agree?

Tuesday, December 7, 2010

Web Services - An Introduction

Hello there,
This article is for the beginners and people who are new to Web Services. I have compiled the things you need to know when you get started with Web Service. Screenshots are available in the attached document.

Web Services Introduction:
Web Services help in exposing the available services in your application to the Web thereby making your application a web application. With web services exchange of data between different applications on different platforms becomes simple. Web Services are application components that communicate using XML + HTTP. Web Services use XML to encode and decode data. It uses SOAP to transport the data.

It is composed of three elements
• SOAP – Simple Object Access Protocol (Platform independent, XML Based)
• UDDI – Universal Description, Discovery and Integration
• WSDL – Web Services Description Language

SOAP – Platform independent, XML based protocol for accessing a web service.
UDDI - A directory service where companies can register and search for Web services.
WSDL - XML based language that describes Web Services message formats and protocols.

Creating Web Services:
Web Services can be created in two ways.
• Top Down Approach (WSDL  Coding)
• Bottom Up Approach (Coding  WSDL)

Top down Approach:
In this method, the WSDL is created first using the WSDL editor. The web service wizard on eclipse can be used to create skeleton Java Classes and the desired implementation can be done on or based out of those classes. Creating the WSDL first gives more control over the web service and hence this is the recommended way of implementing a web service.

Bottom up Approach:
In this method, the desired implementation is done through coding and the service class (Java Bean or EJB) is exposed as Web Service. Web Service wizard in eclipse can help in creating the WSDL file and web service configurations corresponding to the Service class that is exposed. This method is easier and faster and used by developers who are new to Web Service.


IDE / JDK requirement:
  1. Get Eclipse 3.2 or above.
  2. Install JDK 1.4 or above.
  3. Install Tomcat 5.5 or above.
The example project here uses Eclipse 36 (Helios, Java EE IDE for Web Developers), JDK 1.6 and Tomcat 6.0.1.2

Getting Started with Web Service:
Let’s build a web service that accepts a name and says Hello. i.e. If you pass “John”, the web service will return a string “Hello John” as response. The method exposed to web here is “sayHello”.
Top Down Approach:

Create a Workspace with a dynamic web project (2.5).
Copy axis.jar, saaj.jar, jaxrpc.jar to the lib folder of WEB-INF.
Create a folder named “wsdl” inside your WEB-INF. Copy/Paste your wsdl file to that.






















Right Click on the WSDL file (Hello.wsdl), Select Web Services and Create Java Bean Skeleton.






















Web Service Wizard will pop up. Carefully note down the text fields and values. Do not change anything here. Click Next.



































Click Next



































Now your Java Bean classes are created. Click on Start Server. This will add the Web Project (Hello_Web) to the Tomcat server and start the server. You can also see the Web Service Descriptor files created inside Web Project (deploy.wsdd and undeploy.wsdd).





















Bottom Up Approach:

Create a new dynamic web project (Hello_WebWS) .
Copy axis.jar, saaj.jar, jaxrpc.jar to the lib folder inside WEB-INF.
Create a Java Class (com.webservice.sample.Hello.java) with a public method with signature “public String sayHello (String name)”. Let the method return the string “Hello + name”.






















Right Click on Hello.java Web Services Create Web Service






















Web Service Wizard will show up with Web Service type as “Bottom Up” Approach. No changes here. Click Next.


































All your public methods in Hello.java should come here. Select the methods you want to be exposed as Web Service. Click Next.


































You do not want to publish your web service now. So, click finish button here.
Your WSDL file, Web Service Descriptors (deploy.wsdd, undeploy.wsdd) are created on Web Project, and web project added to server and should be in STARTED status now.



Testing Web Services:
Testing the web service is easy and is same for both the development approaches. Right click on the WSDL, Select Test with Web Services Explorer.



































Click on the sayHello method. It will show a text field to accept the input (name). Type in “John” and click on GO. You will see the response printed on the explorer.





















Hope it helps... :)