JSON backed by an XSD schema for correctness. XML is widely used to represent data. XML is often talked of as a human readable format. Yes it is readable. But how often do we need to do that. Use it for its worth…. data representation.
XML Schema files are used to represent the elements that can be contained in an XML file, including data element names, data structures, types, sequence, etc. Given an XSD file it is easy to use available tools and API’s to verify correctness of a document (validity
and well form”ness”).
JSON (JavaScript Object Notation) is another data representation and exchange format. JSON is receving some attention nowadays due to the AJAX speed train. When you think AJAX there are two types of clients:
- AJAX-lite clients. These web pages use XMLHttpRequest or some wrapper framework to make asynchronous calls to the server. They receive back HTML responses which the client then inserts into specific locations in the web page. These applications may sometimes use widget libraries to enhance user experience.
- RIA-AJAX. The asynchronous nature still exists. Always uses rich widget libraries (such as dojo, YUI, backbase, tibco GI, etc). But here the event handling and data manipulation is at the widget level (a widget being anything from a text box to a panel or window).
In the RIA-AJAX applications the browser page never refreshes. The client side RIA-AJAX framework provides support in building applications similar to a traditional Windows desktop application. In this scenario the communication between client and server is not HTML. It is some form that allows representing data. XML is one option and JSON is another. JSON support is built into most browsers. Here is a JSON sample.
1 2 3 4 5 6 7 8 9 |
{ "phone":{ "areaCode":703, "number":777000 }, "age":5, "name":"mathew" } |
The XML would be
1 2 3 4 5 6 7 8 9 |
<root> <phone> <areaCode>703</areaCode> <number>777000</number> </phone> <age>5</age> <name>mathew</name> </root> |
Both formats represent the same data. XML is a little more verbose. For larger files XML is fatty.
It is entirely possible for a web application to send out JSON formatted data to an AJAX client. It is possible to use JSON in a non-UI application too. In either case we need some way to serialize and deserialize JSON to and from Java. Serializing from Java objects to
JSON is relatively simpler (note I say relatively). The real challenge is in deserializing incoming JSON to Java objects. I have written some basic Java-to-JSON serializer (supports basic types, wrapper types, complex java objects and arrays of simple type). But have not tried the other way yet.
Also I wonder if we can use XSD to represent the grammer and rules inside a JSON data file. I see no reason why not. Has anyone tried this. Appreciate any pointers. For example given a JSON data file and XSD is there something that can validate the JSON data file?