GWT Custom Serialization

531 жолу көрүлдү
Биринчи окулбаган билдирүүгө өтүү

Lucas Charron

окула элек,
2010-ж., 25-сен. 12:31:3825/9/10
– Google Web Toolkit
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.

So, other than removing the final attribute, has anyone had any luck
side-stepping this issue? Anyone know of any bug reports or
enhancement requests for this (I have searched, but have come up
empty).

Paul Robinson

окула элек,
2010-ж., 26-сен. 15:31:4126/9/10
– google-we...@googlegroups.com
To deserialise an object, GWT calls the no-argument constructor (which you haven't specified, but is required) and then modifies the fields to match what is required. You can't modify fields if they're marked final, so GWT ignores final fields.

Don't forget that whatever approach is used it needs to (a) support class hierarchies and (b) be translatable to javascript and (c) work in the general case. That's why the gwt serialization rules are the way they are.

If you don't like the rules, you can always create a custom field serializer for each class and handle the serialization yourself.

Paul

Thomas Broyer

окула элек,
2010-ж., 26-сен. 19:27:4526/9/10
– Google Web Toolkit
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.

Lucas Charron

окула элек,
2010-ж., 29-сен. 20:18:4029/9/10
– Google Web Toolkit
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.
Баарына жооп берүү
Авторго жооп берүү
Багыттоо
0 жаңы билдирүү