tags:

views:

307

answers:

4

JAXB works well until I need to do something like serialize beans for which I cannot modify the source. If the bean doesn't have a default constructor or if it refers to objects I want to mark transient then I'm stuck writing a separate bean which I can annotate and then manually copy the information over from the other bean.

For instance, I wanted to serialize exception objects, but found that the only way to do that was use a hack that required using com.sun.* classes.

So, what alternatives are there? What's the next most popular xml serializing api? It would be nice to be able to do things like:

  • Choose at serialization time whether to include certain fields in the result. (marking things transient when running the serializer).

  • Handle loops in the object graph by using references or something other than just dying.

  • Perhaps annotate an object so that in version 1 it serializes things in one way and in version 2 it serializes them in another. Then when serializing I just choose which version of the object ot serialize.

  • Have a way to generate XSDs from annotations on an object.

Basically I just want more flexibility than I currently have with JAXB.

A: 

XStream is a popular XML serialisation library that claims to be able to serialize just about anyting, regardless of constructors or other problems (even deserialize final fields). Give it a try.

Requires no modifications to objects. Serializes internal fields, including private and final. Supports non-public and inner classes. Classes are not required to have default constructor.

Gerco Dries
A: 

Well the standard answer for wanting a uber configurable serialisation framework is xstream.

Gareth Davis
+1  A: 

Also look at JIBX. It's a good xml<->object mapper. My experience is though that if your objects have a somewhat funky relationships it's often easier to create a wrapper object that hides that complexity and then map that object with JIBX.

Chris Kessel
+1  A: 

JAXB is a spec, so you can pick from different implementations. EclipseLink JAXB (MOXy) has extensions for what you are asking:

Externalized Metadata

Useful when dealing with classes for which you cannot annotate the source or to apply multiple mappings to an object model.

XPath Based Mapping

For true meet-in-the-middle OXM mapping:

JPA Compatibility

Including support for bi-directional relationships.

Blaise Doughan