views:

42

answers:

1

I am currently using XStream to serialize some of my objects that don't implement Serializable. Is there a way to tell XStream to use Java's default serialization if the object does implement Serializable and to fall back on XML serialization if it does not? Or would I need to implement a simple layer on top of it to check?

thanks, Jeff

A: 

This would not be a good idea. Java serialization is a binary representation, XML is a textual representation.

Take java.lang.String, for instance. This implements Serializable, but clearly you would not want your Strings serialized as binary blobs inside your XML. Similarly for things like numeric types, etc.

XStream has a mechanism for registering custom converters, I suggest you use that. if you choose to serialize binary data into your XML document, you'll need to encode it somehow, e.g. with Base64 encoding.

skaffman
Good explanation. I wasn't really thinking to apply this down the entire object graph. For example, if I try to serialize class X and it is Serializable, then I would serialize it (and its entire object graph) to a binary file (since it is marked as Serializable, I'll assume its object graph is too). If it is not marked Serializable, I would use xml for that object (and its entire object graph). The reason being is that I have a lot of different objects I'm serializing for persistence but some have huge representations for XML and for those data types I'd rather store in another format.
Jeff Storey