Hi Robert,

Robert Feustel wrote:

> 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?)

Normally neither Java (de-)serialization nor XStream unmarshalling will run 
class initializers or constructors. Possibly Java deserialization makes an 
exception here for this rule for unserializable parents, since it requires 
those to have a default constructor. However, there's no official way to run 
parent's class initializers or default constructors without actually 
constructing an object.

> 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...

?? You mean, you want to define the names of the XML tags used for the 
objects written in your writeObject method? Sorry, this is not possible at 
all. You write the pure objects, so this is all that XStream gets.

If you have special requirements here for your XML, you will have to write a 
complete custom converter(s) for your type(s), have a look at the converter 
tutorial. This is not difficult, but you will have to keep the code in the 
custom converter and the serialization methods logically in sync on your 
own, because those two methods are then no longer relevant for the 
marshalling process of XStream.

> Thank you for all your help,

Your welcome.

Cheers,
Jörg



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

    http://xircles.codehaus.org/manage_email


Reply via email to