April 2, 2015

GWT Custom Serializer

GWT allows you to build your own customer serializers. In the past, I've found issues/bugs with certain serialization of classes and this allows you to provide your own implementation. The key to this is that you have to put your class in the com.google.gwt.user.client.rpc.core + your class package.

The following example you be to create a custom serializer for the my.package.ApiException class.


package com.google.gwt.user.client.rpc.core.my.package;     

public class ApiException_CustomFieldSerializer extends CustomFieldSerializer {

     @Override
     public void deserializeInstance(SerializationStreamReader streamReader, ApiException instance) throws SerializationException {
           deserialize(streamReader,instance);
      }

    @Override
    public void serializeInstance(SerializationStreamWriter streamWriter, ApiException instance) throws SerializationException {
           serialize(streamWriter,instance);
    }
 
    public static void serialize(SerializationStreamWriter streamWriter,ApiException instance) throws SerializationException {
           streamWriter.writeString( instance.getMessage() );
    }
 
    public static void deserialize(SerializationStreamReader streamReader,ApiException instance) throws SerializationException {
           instance.setMessage( streamReader.readString() );
    }
 
    public static ApiException instantiate(SerializationStreamReader reader) throws SerializationException {
          return new ApiException();
    }
}

No comments: