Wednesday, September 29, 2010

Re: GWT Custom Serialization

I did eventually find documentation on the custom field serialization
API. It is my belief that it is a bad approach.

I do not understand why they did not make a marker interface, let's
call it "HasCustomSerialization", that GWT would use to identify
classes with custom serialization. Example:
public class Example implements IsSerializable,
HasCustomSerialization
{
private final int a;
private final int b;
public Example(int a,int b)
{
this.a=a;
this.b=b;
}

//Serialization Methods
private static Example instantiate(...){...}
private static void deserialize(...){...}
private static void serialize(...){...}

// getters ...
}

This puts all of the methods required for serialization in one place,
instead of making a shadow class. Additionally, this overcomes
problems with private inner classes - the other serialization method
cannot directly instantiate a private inner class of the class it is
serializing/de-serializing.

On Sep 26, 7:27 pm, Thomas Broyer <t.bro...@gmail.com> wrote:
> On 25 sep, 18:31, Lucas Charron <raijinse...@gmail.com> wrote:
>
>
>
>
>
> > I am having a slight problem with a framework I am making. Here is an
> > example class:
> > public class Example implements IsSerializable
> > {
> > private final int a;
>
> > private final int b;
>
> > public Example(int a,int b)
> > {
> > this.a=a;
> > this.b=b;
>
> > }
>
> > // getters ...
>
> > }
>
> > A very simple class. However, there is no way to serialize it using
> > GWT. Although the simple work-around is to remove the "final"
> > attribute from the fields, that does not "jive" with, what I consider,
> > solid programming. The "final" attribute means that no where, in any
> > instance of the class or any instance of a descendant of this class,
> > will the values of "a" and "b" be modified in any way (exception:
> > using Java reflection). More than the optimization that many JVMs will
> > do, there is also a type of safety - the programmer cannot
> > accidentally put the reference on the left hand side of an assignment
> > operator.
>
> > The GWT compiler will emit a warning that the final fields will not be
> > serialized. Using the "_CustomFieldSerializer" is also impossible
> > because the deserialize method takes an instance of the class you are
> > deserializing as a parameter - instead it should have the return value
> > as an instance of the class.
>
> You can use an instantiate method in _CustomFieldSerializer. It works
> the same as deserialize but returns a newly created instance (and then
> deserialize is called with that instance).
>
> public static Example instantiate(SerializationStreamReader reader)
> throws SerializationException {
>    return new Example(reader.readInt(), reader.readInt());
>
> }
>
> Even if you don't need it, the deserialize method is required,
> contrary to the instantiate method.

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

No comments:

Post a Comment