2

This may be a repeat question but I haven't been able to find a solution. I have some complex java object and I want to serialize it to json string and then I want to pass it to Rest web services from GWT client. For calling rest web services I am using RequestBuilder class or RPC proxy. The problem is I haven't been able to find a solution to convert Object to json I tried using AutoBean framework but I have read on somewhere on stack overflow that it is very buggy. The Object that I want to convert can be any inbuilt Java serialize able object String, HashMap, ArrayList or it can be custom POJO object. The project that I am working on is very large and I want a robust solution. I tried this

AutoBean<HashMap> bean = AutoBeanUtils.getAutoBean(myMap);
return AutoBeanCodex.encode(bean).getPayload();

This is returning null string

user123
  • 518
  • 12
  • 24
  • [Using Gson library in GWT client code](http://stackoverflow.com/questions/2213734/using-gson-library-in-gwt-client-code) you can solve this problem – Braj Mar 14 '14 at 20:32
  • 1
    GSON will not work in Clientcode. (If it is not changed in the last months) – Christian Kuetbach Mar 14 '14 at 21:24
  • @Braj As ChristianKuetbach has already mentioned GSon will not work there is a library though bGWTGson https://github.com/heroandtn3/bGwtGson This is also Using RPC architecture at backend. – user123 Mar 15 '14 at 11:57
  • But this will create a client server communication for serialization. – Christian Kuetbach Mar 16 '14 at 01:11
  • did you try a custom jsni method? `private native String stringfy(Object obj) /*-{ return JSON.stringify(obj); }-*/;` – Marc Stroebel Nov 09 '22 at 13:42

3 Answers3

0

AutoBean can serialize Lists and Maps, BUT only if you have a top level interface you know.

Simply serializing all kind of serializable is not possible with JSON and GWT (as far as I Know).

GWT-RPC can do this job, because it will generate code at compile time and has access to the Java-Reflection API.

I don't know any GWT clientcode compatible JSON serializer which is able to serialize every Serializable Object.

Christian Kuetbach
  • 15,850
  • 5
  • 43
  • 79
  • Technically, AutoBeans also generates code at compile time and uses reflection to work out what is reachable/makes sense, but this further means that neither rpc nor autobeans can handle arbitrary objects that they haven't been informed of. Without compiling in all possible types and all field metadata, you can't teach a gwt project to serialize any arbitrary Object subclass. You *could* compile in all of that, but it would potentially seriously bloat the compiled app size. – Colin Alworth Mar 14 '14 at 23:48
0

After working on this for some time The solution that I had finally thought of. I used GWT RPC proxy to call rest services. In my impl class I am using jackson to Serialize Objects than using Jersey client API I am consuming rest services than again deserialize response and pass it to client. This is the best solution that I can think of till now. Jackson and jersey are production ready libraries. This way I have two advantages. 1. I dont have to mess with the client side code. 2. Cross domain rest call is possible (SOP limitation ).

Any suggestions are welcome.

Hope this helps

user123
  • 518
  • 12
  • 24
0

I've seen the most success and least amount of code using this library:

https://code.google.com/p/gwtprojsonserializer/

Along with the standard toString() you should have for all Object classes, I also have what's called a toJsonString() inside of each class I want "JSONable". Note, each class must extend JsonSerializable, which comes with the library:

public String toJsonString()
{
    Serializer serializer = (Serializer) GWT.create(Serializer.class);

    return serializer.serializeToJson(this).toString();
}

To turn the JSON string back into an object, I put a static method inside of the same class, that recreates the class itself:

public static ClassName recreateClassViaJson(String json)
{
    Serializer serializer = (Serializer) GWT.create(Serializer.class);

    return (ClassName) serializer.deSerialize(json, "full.package.name.ClassName");
}

Very simple!

Jason Washo
  • 536
  • 6
  • 22