Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Json <-> Java serialization that works with GWT [closed]

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) 
like image 827
amartynov Avatar asked Mar 25 '09 19:03

amartynov


People also ask

How does JSON serialization work in Java?

JSON-Java is a Java serialization/deserialization library. It parses JSON documents into Java objects and generates new JSON documents from the Java classes.

Can JSON be serialized?

JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation (convert string -> object). If you serialize this result it will generate a text with the structure and the record returned.

Is Java serialization faster than JSON?

The results show that Java serialization is slow for a small number of objects, but good for a large number of objects. Conversely, XML and JSON can outperform Java serialization for a small number of objects, but Java serialization is faster for a large number of objects.

How do you serialize and deserialize JSON object in Java?

Deserialization in the context of Gson means converting a JSON string to an equivalent Java object. In order to do the deserialization, we need a Gson object and call the function fromJson() and pass two parameters i.e. JSON string and expected java type after parsing is finished.


2 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()); 
like image 62
Chris Kentfield Avatar answered Sep 23 '22 17:09

Chris Kentfield


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).

like image 42
rjrjr Avatar answered Sep 21 '22 17:09

rjrjr