GWT Object Serialization with gwt-storage

Problem

The built in Java Serialization and various serialization frameworks available for Java allow to serialize Java objects conveniently into binary or textual representations. Unfortunately, most of these frameworks are not available for Google Web Toolkit client applications.

There is no easily available built in solution for serializing GWT objects into text on the GWT client side. This is surprising since GWT evidently has a mechanism to serialize and deserialize objects to support GWT RPC.

Solution

Seanchenxi has kindly created a convenient wrapper around the GWT RPC serialization mechanism, which makes it very easy to serialize and deserialize objects on a GWT client application into String representations. The project, gwt-storage, is available on github:

https://github.com/seanchenxi/gwt-storage

You can download the source code and import it to your Java project. I’ve also uploaded the project to a public Maven repository. You can add it to your Maven project as follows:


<dependencies>

<dependency>

<groupId>com.seanchenxi.gwt</groupId>

<artifactId>gwt-storage</artifactId>

<version>1.2</version>

</dependency>

</dependencies>

…

<repositories>

<repository>

<id>Appjangle</id>

<url>http://maven.appjangle.com/appjangle/releases/</url>

</repository>

</repositories>

With gwt-storage available for your project, you can do the following in GWT client side code:


StorageSerializer serializerImpl = new com.seanchenxi.gwt.storage.client.serializer.StorageRPCSerializerImpl();

try {

MyClass obj = new MyClass("1234");

String serialized = serializerImpl.serialize(Serializable.class, obj);

GWT.log("Serialized Object: "+serialized);

MyClass deserialized = (MyClass) serializerImpl.deserialize(

Serializable.class, serialized);

} catch (SerializationException e) {

throw new RuntimeException(e);

}

Note:

  • If you download the gwt-storage source code, you might have to change the visibility of the class StorageRPCSerializerImpl() to public (from package).
  • MyClass needs to be serializable by GWT RPC; that is, implement Serializable and have non-transient references that are Serializable as well.
  • If you change the implementation of the class MyClass, deserialization of Strings created with older versions of the class will probably not be possible.

References

Serializing objects in GWT and deserializing them in servlet

GWT Google Groups ‘GWT client side Java object serialization’

GWT RPC Serialization for LocalStorage

GWT RPC is a great technology for sending ‘Java’ objects from a Java sever to a JavaScript client and vice averse. Since GWT RPC provides facilities for serializing and deseralizing Java objects, it seems like a good option, too, for preparing objects to be stored in a browsers LocalStorage.

Unfortunately, the devil lies in the details here, since GWT RPC is implemented in an asymmetrical way:

  • The client can only deseralize objects serialized on the server and
  • Only the server can deseralize objects serialized on the client.

Fortunately, however, with a few modifications the GWT RPC serialization mechanism can be adjusted to support client-client serialization.

The gwt-storage project available on GitHub does exactly that for us. Check out the getting started guide for this project here. Here a simple example:

StorageExt localStorage = StorageExt.getLocalStorage();

localStorage.put(key, gwtObject);


GwtClass gwtObject = localStorage.get(key);

Note that for this code to work, GwtClass must be involved in one of your GWT RPC services.

Further Information

Stackoverflow ‘How to apply SerializationStreamWriter for storage’

GWT Docs class RPC

SerializationStreamWriter (client)