Json <-> Java serialization that works with GWT

I am looking for a simple Json (de)serializer for Java that might work with GWT. I have googled a bit and found some solutions that either require annotate every member or define useless interfaces. Quite a boring. Why don't we have something really simple like

class MyBean {
    ...
}

new GoodSerializer().makeString(new MyBean());
new GoodSerializer().makeObject("{ ... }", MyBean.class)

Answers


Take a look at GWT's Overlay Types. I think this is by far the easiest way to work with JSON in GWT. Here's a modified code example from the linked article:

public class Customer extends JavaScriptObject {
    public final native String getFirstName() /*-{ 
        return this.first_name;
    }-*/;
    public final native void setFirstName(String value) /*-{
        this.first_name = value;
    }-*/;
    public final native String getLastName() /*-{
        return this.last_name;
    }-*/;
    public final native void setLastName(String value) /*-{
        this.last_name = value;
    }-*/;
}

Once you have the overlay type defined, it's easy to create a JavaScript object from JSON and access its properties in Java:

public static final native Customer buildCustomer(String json) /*-{
    return eval('(' + json + ')');
}-*/;

If you want the JSON representation of the object again, you can wrap the overlay type in a JSONObject:

Customer customer = buildCustomer("{'Bart', 'Simpson'}");
customer.setFirstName("Lisa");
// Displays {"first_name":"Lisa","last_name":"Simpson"}
Window.alert(new JSONObject(customer).toString());

Another thing to try is the new AutoBean framework introduced with GWT 2.1.

You define interfaces for your beans and a factory that vends them, and GWT generates implementations for you.

interface MyBean {
  String getFoo();
  void setFoo(String foo);
}

interface MyBiggerBean {
  List<MyBean> getBeans();
  void setBeans(List<MyBean> beans>;
}

interface Beanery extends AutoBeanFactory{
  AutoBean<MyBean> makeBean();
  AutoBean<MyBiggerBean> makeBigBean();
}

Beanery beanFactory = GWT.create(Beanery.class);

void go() {
  MyBean bean = beanFactory.makeBean().as();
  bean.setFoo("Hello, beans");
}

The AutoBeanCodex can be used to serialize them to and from json.

AutoBean<MyBean> autoBean = AutoBeanUtils.getAutoBean(bean);
String asJson = AutoBeanCodex.encode(autoBean).getPayload();

AutoBean<MyBean> autoBeanCloneAB = 
  AutoBeanCodex.decode(beanFactory, MyBean.class, asJson );

MyBean autoBeanClone = autoBeanCloneAB.as(); 
assertTrue(AutoBeanUtils.deepEquals(autoBean, autoBeanClone));

They work on the server side too — use AutoBeanFactoryMagic.create(Beanery.class) instead of GWT.create(Beanery.class).


The simplest way would be to use GWT's built-in JSON API. Here's the documentation. And here is a great tutorial on how to use it.

It's as simple as this:

String json = //json string
JSONValue value = JSONParser.parse(json);

The JSONValue API is pretty cool. It lets you chain validations as you extract values from the JSON object so that exceptions will be thrown if anything's amiss with the format.


It seems that I found the right answer to my question

I figured out that bean to json and json to bean conversion in GWT isn't a trivial task. Known libraries would not work because GWT would require their full source code and this source code must use only Java classes that are amoung emulated by GWT. Also, you cannot use reflection in GWT. Very tough requirements!

I found the only existing solution named gwt-jsonizer. It uses a custom Generator class and requires a satellite interface for each "jsonable" bean. Unfortunately, it does not work without patching on the latest version of GWT and has not been updated for a long time.

So, I personally decided that it is cheaper and faster to make my beans khow how to convert themselves to and from json. Like this:

public class SmartBean {
    private String name;

    public String getName() { return name; }
    public void setName(String value) { name = value;  }

    public JSONObject toJson() {
        JSONObject result = new JSONObject();
        result.put("name", new JSONString(this.name));
        return result;
    }
    public void fromJson(JSONObject value) {
        this.name = value.get("name").isString().stringValue();
    }

}

JSONxxxx are GWT built-in classes that provide low-level json support.


RestyGWT is a powerful library for encoding or decoding Java Object to JSON in GWT:

import javax.ws.rs.POST;
...
public interface PizzaOrderCodec extends JsonEncoderDecoder<PizzaOrder> {
}

Then:

// GWT will implement the interface for you
PizzaOrderCodec codec = GWT.create(PizzaOrderCodec.class);

// Encoding an object to json
PizzaOrder order = ... 
JSONValue json = codec.encode(order);

// decoding an object to from json
PizzaOrder other = codec.decode(json);

It has also got several easy to use API for consuming Restful web services.

Have a nice time.


Check this:

GWT Professional JSON Serializer: http://code.google.com/p/gwtprojsonserializer/

!Works with GWT 2.0+!


json.org/java seems to be included with GWT these days:

gwt-servlet-deps.jar\org\json\

Or, this project seems to be comprehensive: http://code.google.com/p/piriti/


In Google Web Toolkit Applications, pages 510 to 522, the author, Ryan Dewsbury, shows how to use GWT code generation to do serialization to and from XML and JSON documents.

You can download the code here; you want the chapter 10 code bundles, and then you want to look in the src/com/gwtapps/serialization package. I did not see a license for this code, but have emailed the author to see what he says. I'll update this if he replies.

Issues with this solution:

  • You have to add a marker interface on all your objects that you want serialized (he uses java.io.Serializable but I imagine you could use others--if you are using hibernate for your backend, your pojos might already be tagged like this).
  • The code only supports string properties; it could be extended.
  • The code is only written for 1.4 and 1.5.

So, this is not an out of the box solution, but a great starting point for someone to build a JSON serializer that fits with GWT. Combine that with a JSON serializer on the server side, like json-lib and you're good to go.

I also found this project (again, some marker interface is required).


Try this serializer from Google Code: http://code.google.com/p/json-io/

If you need to write or read JSON format in Java, this is the tool to use. No need to create extra classes, etc. Convert a Java object graph to JSON format in one call. Do the opposite - create a JSON String or Stream to Java objects. This is the fastest library I have seen yet to do this. It is faster than ObjectOutputStream and ObjectInputStream in most cases, which use binary format.

Very handy utility.


You may want to checkout this project https://gerrit.googlesource.com/gwtjsonrpc/

It's a library created in order to support a code review system for Android, Gerrit, but it's a stand-alone module meant to be embedded into any GWT project, not just Gerrit.

A reasonable tutorial is probably the README in the top level of the directory. It's quite similar to standard GWT RPC but it uses JSON encoding. It also has built-in XSRF protection.


I seem to be answering this question a lot...

There's a page on code.google.com titled Using GWT for JSON Mashups. It's (unfortunately) way over my head, as I'm not that familiar with GWT, so it may not be helpful.


OK, I deleted my previous answer because it turned out to be exactly what you didn't want.

I don't know how well it works with GWT, but we use the json-lib library to serialize objects in a normal Java project where I work.

It can create a JSONObject directly from a JavaBean, then use the resulting JSONObject's toString() method to get the actual JSON string back.

Likewise, it can also turn JSON back into a JavaBean.


Not sure if Jackson would work for you. I don't know if there's GWT-specific you are looking for; if not it should work.

But its serialization/deserialization works quite well, like:

// read json, create object
ObjectMapper mapper = new ObjectMapper();
MyBean bean = mapper.readValue(jsonAsString, MyBean.class);

// and write out
StringWriter sw = new StringWriter();
mapper.writeValue(sw, user);
String jsonOut = sw.toString();

You do need accessors (getX() to serialize, setX() to deserialize; can annotate methods with other names), but that's about it.


Need Your Help

Intuitive way of understanding hexadecimal html color codes?

html colors mnemonics

Is there an intuitive way, or a good mnemonic, for understanding the correspondence between colors and their hexadecimal values?

2D Geometry library: LGPL alternative to CGAL?

c++ geometry computational-geometry lgpl

CGAL seems to do just about everything I need and a little more for my upcoming project. It can create polygons out of arc line segments and run boolean operations on them. It has spatial sorting