0

I am trying to serialize a object in GWT using SerializationFactory, but I am not able to get it working. Here is the sample code of my POC:

    import com.google.gwt.user.client.rpc.SerializationException;
    import com.google.gwt.user.client.rpc.SerializationStreamFactory;
    import com.google.gwt.user.client.rpc.SerializationStreamReader;
    import com.google.gwt.user.client.rpc.SerializationStreamWriter;
...........
Some code here....
.........

......

SerializationStreamFactory factory = (SerializationStreamFactory) GWT.create(MyClass.class);
    SerializationStreamWriter writer = factory.createStreamWriter();

    try {
        writer.writeObject(new MyClass("anirudh"));
        String value = writer.toString();


        SerializationStreamReader reader = factory.createStreamReader(value);

        MyClass myObj = (MyClass) reader.readObject();
        System.out.println(myObj.getName());
    } catch (SerializationException e) {
        e.printStackTrace();
    }   

It gave me the following exception

Caused by: java.lang.RuntimeException: Deferred binding failed for 'com.anirudh..client.MyClass' (did you forget to inherit a required module?)

also in my code the class whose object I am trying to serialize implements IsSerializable

MyClass implements IsSerializable

I don't want to use GWT Auto-Bean framework because it does not fit my use case. Also I am not using GWT-RPC framework and right now I am quite adamant about using SerializationStreamFactory :D because I seriously want to know how this thing works.

Can anyone share a working example of SerializationStreamFactory or help me out pointing any mistake(s) I did. Thanks in advance

Durin
  • 2,070
  • 5
  • 23
  • 37

1 Answers1

2

SerializationStreamFactory factory = (SerializationStreamFactory) GWT.create(MyClass.class);

What are you expecting this line to do? GWT will attempt to find a replace-with or generate-with rule that matches this class (either when-type-assignable or when-type-is), or failing that will attempt to invoke a zero-arg constructor on MyClass, effectively new MyClass(). Is this what you are expecting?

The selected exception you've pasted suggests that MyClass may not be on the source path that GWT has been given to compile from, but the full error log will provide more information.

It looks as though you are trying to mimic the generated RPC code, where a *Async rpc interface would be implemented by code that extends from com.google.gwt.user.client.rpc.impl.RemoteServiceProxy (which implements SerializationStreamFactory). That base implementation is extended further to initialize several fields such as the com.google.gwt.user.client.rpc.impl.Serializer instance, actually responsible for serializing and deserializing object streams.

Serializers are created (by default) from the base class of com.google.gwt.user.client.rpc.impl.SerializerBase, through the rebind class com.google.gwt.user.rebind.rpc.TypeSerializerCreator. If you've build your own generator for MyClass, you should be kicking this off to get the work done as ProxyCreator already should be doing.

Remember when building your own serialization/deserialization mechanism that you need to decide which types can be marshalled within this system - if you open it to all types, then you will need to generate FieldSerializer types for all possible objects on the source path. This will greatly expand the size of your compiled code.

If your main goal is learning how this 'magic' works, dig into the generators and associated code that live in the com.google.gwt.user.rebind.rpc package. There are other libraries that leverage these ideas such as the gwt-atmosphere project (see https://github.com/Atmosphere/atmosphere to get started). Also review the generated code that GWT creates when it builds a 'tradition' RPC interface.

Colin Alworth
  • 17,801
  • 2
  • 26
  • 39