Json & # 8596; Java serialization that works with GWT

I am looking for a simple Json (de) serializer for Java that can work with GWT. I searched Google a bit and found some solutions that either require annotating each member or defining useless interfaces. Pretty boring. Why don't we do something really simple, like

class MyBean { ... } new GoodSerializer().makeString(new MyBean()); new GoodSerializer().makeObject("{ ... }", MyBean.class) 
+58
json serialization javabeans marshalling gwt
Mar 25 '09 at 19:40
source share
13 answers

Take a look at GWT Overlay Types . I think this is the easiest way to work with JSON in GWT. Here is an example of a modified code from a related 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; }-*/; } 

After determining the type of overlay, it is 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 a re-presentation of the JSON object, 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()); 
+55
Apr 10 '09 at 7:03
source share

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

You define the interfaces for beans and factory that decrypt 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"); } 

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 also work on the server side - use AutoBeanFactoryMagic.create(Beanery.class) instead of GWT.create(Beanery.class) .

+38
Feb 17 '11 at 21:44
source share

The easiest way is to use the built-in JSON GWT API. Here is the documentation . And here is a great tutorial on how to use it.

It is so simple:

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

The JSONValue API is pretty cool. It allows you to check the chain when retrieving values ​​from a JSON object so that exceptions are thrown if something is not consistent with the format.

+12
Jul 17 '10 at 5:04 on
source share

I seem to have found the right answer to my question

I realized that converting bean to json and json to bean in GWT is not a trivial task. Well-known libraries will not work because GWT will require their full source code, and this source code should only use Java classes that emulate GWT. In addition, you cannot use reflection in GWT. Very strict requirements!

I found the only existing solution called gwt-jsonizer . It uses its own Generator class and requires a satellite interface for each jsonable bean. Unfortunately, it does not work without fixing the latest version of GWT and does not update for a long time.

So, I personally decided it was cheaper and faster to make my khow beans how to convert myself 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.

+10
Mar 26 '09 at 8:05
source share

RestyGWT is a powerful library for encoding or decoding Java Object for 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 also has several easy-to-use APIs for consuming Restful web services.

Have a good time.

+6
Dec 15 '12 at 11:57
source share

Check this:

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

! Works with GWT 2.0+!

+5
Apr 01 '10 at 7:13
source share

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

GWT-servlet-deps.jar \ org \ JSON \

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

+3
Dec 16 '10 at 17:11
source share

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

You can download the code here ; you need the chapter 10 code suites, and then you want to look in the src / com / gwtapps / serialization package. I did not see a license for this code, but sent it by email to find out what it says. I will update this if he answers.

Problems with this solution:

  • You need to add a marker interface to all of your objects that you want to serialize (it uses java.io.Serializable, but I suppose you could use others - if you use hibernation for your backend, your fields may already be marked like this )
  • The code only supports string properties; It can be expanded.
  • The code is written only for 1.4 and 1.5.

So this is not a solution, but a great starting point for a JSON compiler that is suitable for GWT. Combine this with a server-side JSON serializer like json-lib , and you're good to go.

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

+2
Nov 17 '09 at 17:58
source share

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

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

Very handy utility.

+1
Oct. 21 2018-10-21
source share

It seems that I answer a lot to this question ...

There's a page on code.google.com called Using GWT for JSON Mashups . This (unfortunately) is over my head, as I am not familiar with GWT, so it may not be useful.

0
Mar 25 '09 at 21:04
source share

You can check this project https://gerrit.googlesource.com/gwtjsonrpc/

This is a library designed to support the Android code review system, Gerrit, but it is a standalone module designed to be embedded in any GWT project, not just Gerrit.

A reasonable guide is probably README at the top level of the directory. It is very similar to the standard GWT RPC, but uses JSON encoding. It also has built-in XSRF protection.

0
Apr 6 2018-11-11T00:
source share

OK, I deleted my previous answer because it turned out to be exactly what you did not want.

I do not know how well it works with GWT, but we use the json-lib library to serialize objects in the normal Java project in which I work.

He can create a JSONObject directly from the JavaBean , and then use the resulting JSONObject toString () method to return the actual JSON string.

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

-one
Mar 25 '09 at 20:11
source share

Not sure if Jackson will work for you. I do not know if there is a GWT-specific that you are looking for; if not, then it should work.

But its serialization / deserialization works pretty well, for example:

 // 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 need accessors ( getX() for serialization, setX() for deserialization, can annotate methods with different names), but more on that.

-one
Mar 26 '09 at 2:45
source share



All Articles