benstpierre
benstpierre

Reputation: 33581

Way to serialize a GWT client side object into a String and deserialize on the server?

Currently our application uses GWT-RPC for most client-server communication. Where this breaks down is when we need to auto generate images. We generate images based on dozens of parameters so what we do is build large complex urls and via a get request retrieve the dynamically built image.

If we could find a way to serialize Java objects in gwt client code and deserialize it on the server side we could make our urls much easier to work with. Instead of

http://host/page?param1=a&param2=b&param3=c....

we could have

http://host/page?object=?JSON/XML/Something Magicical

and on the server just have

new MagicDeserializer.(request.getParameter("object"),AwesomeClass.class);

I do not care what the intermediate format is json/xml/whatever I just really want to be able stop keeping track of manually marshalling/unmarshalling parameters in my gwt client code as well as servlets.

Upvotes: 2

Views: 3920

Answers (2)

Jason Washo
Jason Washo

Reputation: 615

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!

Upvotes: 0

Boris Daich
Boris Daich

Reputation: 2461

Use AutoBean Framework. What you need is simple and is all here http://code.google.com/p/google-web-toolkit/wiki/AutoBean

Upvotes: 3

Related Questions