Summary
JSON, the JavaScript Object Notation, is becoming an increasingly popular format for transmitting data between applications. In his recent OnJava article, Dejan Bosanac shares his experience with Java-centric JSON APIs.
Advertisement
Part of the attractiveness of JSON, the JavaScript Object Notation, stems from its ability to encode data in a format that can be directly evaluated in JavaScript. That's because JSON itself is based on a subset of JavaScript, and hence JSON code is also valid JavaScript.
But a more interesting aspect of JSON lies in its simplicity: It contains only two types of data structures, and the entire language is described in a few paragraphs. That simplicity makes JSON ideal as a format to exchange data between applications not based on JavaScript—between two Java programs, for example.
In his recent OnJava article, Java and JSON, Dejan Bosanac describes alternative APIs for encoding Java objects to JSON, and for parsing JSON notation back into Java objects:
JSON in Java looks nice, but what I really want is a more automated and configurable library like those we have for XML processing. My first thought was XStream since I like its simple API, extensible architecture, powerful converters and support for annotations. After a first check I found that from version 1.2 on there is a partial support for JSON (which was the logical thing to expect). So, currently you can serialize your Java objects to JSON format... Unfortunately, there is no read support at this moment for JSON format.
Providing support for reading JSON back into Java objects required a different approach, though, according to Bosanac:
I stumbled upon Jettison project which implements "a collection of Stax parsers and writers which read and write JSON." So instantly I wanted to create a XStream driver that uses Jettison as an underlying library to parse to and from JSON...
It was a little bit trickier then I first thought... [but] it worked at least for the usage that I needed at the moment.
Using Jettison with Bosanac's patches, reading and writing Java classes to JSON and back is rather simple:
public class JSONWrite {
public static void main(String[] args) {
Product product = new Product("Banana", "123", 23.00);
XStream xstream = new XStream(new JettisonDriver());
String result = xstream.toXML(product);
System.out.println(result);
}
}
public class JSONRead {
public static void main(String[] args) {
String json = "{\"org.sensatic.jqr.json.Product\": {"
+ "\"name\": \"Banana\","
+ "\"id\": \"123\","
+ "\"price\": \"23.0\"" + "}}";
XStream xstream = new XStream(new JettisonDriver());
Product product = (Product)xstream.fromXML(json);
System.out.println(product);
}
}
What other libraries or methods to convert between Java and JSON have you found useful?
This approach was very appealing, until we found DWR. DWR is something like a bridge between the world of client side javascript and server side java. You expose a set of objects on the server to clients, when methods on those are called DWR converts the javascript objects given as arguments to java objects on the server and converts result values to javascript objects. Makes AJAX easy. =)