include reflected serialVersionUID to XStream

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

include reflected serialVersionUID to XStream

uwe schaefer
Hi

i'd like to add a reflected serialVersionUID field to the XML XStream
creates/reads from in order to be able to do conversion based on this.

i am puzzled of how to do that in a general manner rather than adding
converters for any known class (i do not know which classes will be
serialized in the future)

i am sure it is easy, but can you point me to the right corner?

thx uwe

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: include reflected serialVersionUID to XStream

Jörg Schaible-2
Hi Uwe,

uwe schaefer wrote:

> Hi
>
> i'd like to add a reflected serialVersionUID field to the XML XStream
> creates/reads from in order to be able to do conversion based on this.
>
> i am puzzled of how to do that in a general manner rather than adding
> converters for any known class (i do not know which classes will be
> serialized in the future)
>
> i am sure it is easy, but can you point me to the right corner?

XStream is a serializer for Java objects and works like Java serialization
i.e. it marshals/unmarshals the data of an instance. Static data is never
part of an instance, therefore it is not supported. What should XStream do
with such data at deserialization anyway?

However, you're always free to implement an own converter and write into the
stream whatever you want. In your case it seems enough to replace the
SerializationConverter that handles any Serializable instances (except
Externalizable and type with other specialized converters). All you have to
do is to derive from SerializableConverter:

=============== %< =============
 class SerializableWithUIDConverter extends SerializableConverter {
   SerializableWithUIDConverter(Mapper mapper, ReflectionProvider
reflectionProvider, ClassLoaderReference classLoaderReference) {
        super(mapper, reflectionProvider, classLoaderReference);
    }
    public void doMarshal(Object source, HierarchicalStreamWriter writer,
MarshallingContext context) {
      long servialVersionUID = FieldUtils.getLong(source.getClass(),
"servialVersionUID");
      writer.addAttribute("SUID", String.valueOf(servialVersionUID));
      super.doMarshal(source, writer, context);
    }
    // no idea yet, what you want to do in unmarshal,
    // but you will have to overwrite it

    // if you want to do this only for known classes you might overwrite
    // method canConvert(Class) and register the converter with normal prio.
 }
=============== %< =============
 xstream.registerConverter(
   new SerializableWithUIDConverter(xstream.getMapper(),
     xstream.getReflectionProvider(), xstream.getClassLoaderReference()),
   XStream.PRIORITY_LOW);
=============== %< =============

If you have Externalizable types, you will have to reregister the
ExternalizableConverter again with low priority.

Cheers,
Jörg


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email