All right,

This pretty much works. Only deserialization doesn't work quite allright, but I have found the problem and multiple solutions to that. So, here's what I've done:

class ProperSerializableConverter extends SerializableConverter {
   ProperSerializableConverter(Mapper mapper, ReflectionProvider
reflectionProvider, ClassLoaderReference classLoaderReference) {
     super(mapper, reflectionProvider, classLoaderReference);
   }
   boolean canConvert(Class type) {
     if (type.getPackage() != null && type.getPackage().startswith(yourPackagePath) 
&& type instanceof Serializable)
       return true;
     return false; }
   protected void marshalUnserializableParent(HierarchicalStreamWriter
writer, MarshallingContext context, Object replacedSource) {
     // do nothing on purpose
   }
 }


This will make it work for all my classes easily. However, I have encountered a little oddity:

abstract class A {

  private LinkedList<String> stringList = new LinkedList<String>();

}

class B extends A implements Serializable {

// some properties and de-/serialization methods

}

Serializing and deserializing an Object of class B through java will work. Through xstream however the LinkedList in class A will not be initialized during deserialization. I don't know what java and xstream do or don't do here. Maybe you can have a look at this. I don't mind working around it. I already have two ideas I will try on monday, though. (Do I have to write a deserializer for that?)

However I've encountered another (more important) problem.
If I use my custom serialization, I don't get the property names... Yeah, I didn't consider that... So now here's a new question: Is it possible to not use the custom serialization, even if there is one? Or, is it possible to somehow feed the property names into the process? Although I wouldn't know how that would be done without breaking java serialization...

Thank you for all your help,

Robert.

Am 13.03.2014 01:10, schrieb Jörg Schaible:
Hi Robert,

Actually, it is ... since XStream serializes any type, not just Serializable
ones. And XStream cannot tell, whether the implementation of a Serializable
type actually handles such a case on purpose or the type was never meant to
be serialized with Java serialization and therefore does not handle the
situation.

It's nothing you can configure, but you can create a customized
SerializationConverter:

================ %< ===========
  class ProperSerializableConverter extends SerializableConverter {
    ProperSerializableConverter(Mapper mapper, ReflectionProvider
reflectionProvider, ClassLoaderReference classLoaderReference) {
      super(mapper, reflectionProvider, classLoaderReference);
    }
    boolean canConvert(Class type) {
      return type == YourType.class;
    }
    protected void marshalUnserializableParent(HierarchicalStreamWriter
writer, MarshallingContext context, Object replacedSource) {
      // do nothing on purpose
    }
  }

  xstream.registerConverter(
    new ProperSerializableConverter(
      xstream.getMapper(),
      xstream.getReflectionProvider(),
      xstream.getClassLoaderReferenfce()));
================ %< ===========
I did not test the solution above, but it should be working since the whole
unserializable-parent element is simply omitted.

Cheers,
Jörg

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

   http://xircles.codehaus.org/manage_email


Reply via email to