The Artima Developer Community
Sponsored Link

Java Community News
Java to JSON and Back

5 replies on 1 page. Most recent reply: May 12, 2009 7:13 AM by Know Something

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
Frank Sommers

Posts: 2642
Nickname: fsommers
Registered: Jan, 2002

Java to JSON and Back Posted: Jan 12, 2007 11:46 AM
Reply to this message Reply
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?


Tobias Mattsson

Posts: 6
Nickname: tobiasm
Registered: Jan, 2007

Re: Java to JSON and Back Posted: Jan 15, 2007 6:01 AM
Reply to this message Reply
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. =)

Tobias Mattsson

Posts: 6
Nickname: tobiasm
Registered: Jan, 2007

Re: Java to JSON and Back Posted: Jan 15, 2007 6:02 AM
Reply to this message Reply
And you can get DWR at: http://getahead.ltd.uk/dwr

Jerry Kurian

Posts: 6
Nickname: javaj
Registered: Jan, 2007

Re: Java to JSON and Back Posted: Jan 15, 2007 6:58 AM
Reply to this message Reply
How does DWR compare with similar tools from google and yahoo. Is there any article comparing them

regards,
Jerry

David Beutel

Posts: 29
Nickname: jdb
Registered: May, 2003

Re: Java to JSON and Back Posted: Jan 19, 2007 5:54 PM
Reply to this message Reply
I wonder if the Java 6 support for JavaScript can be leveraged for any of this.

Know Something

Posts: 1
Nickname: knowsome
Registered: May, 2009

Re: Java to JSON and Back Posted: May 12, 2009 7:13 AM
Reply to this message Reply
There is a nice small java program that does this.
Here it is
http://json2java.blogspot.com/

Flat View: This topic has 5 replies on 1 page
Topic: Rich Hickey Releases Clojure 1.0 Previous Topic   Next Topic Topic: New WebKit 3 Features SVG, XPath, Developer Tools

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use