Skip to content

Add support for JAXB's @XmlSeeAlso (maybe similar to @JsonSubTypes) to improve JAXB compatibility #195

Open
@raphw

Description

@raphw

Problem description

Jackson's XML annotation handler does not currently process the XmlSeeAlso of the JAXB specification. As a result, it is always possible to serialize objects based on JAXB, but it is not possible to deserialize them in all cases. This problem occurs for example if JAXB objects are created by the XJC tool from XSD files that include subtyping.

For example, the following class structure represents a valid JAXB object representation:

class Root {
  Base element;
}

@XmlSeeAlso({First.class, Second.class})
abstract class Base { }

@XmlType(name = "FirstType")
class First extends Base { }

@XmlType(name = "SecondType")
class Second extends Base { }

If base is set to First, JAXB will declare a property in the XML-instance namespace which allows to serialize and to deserialize any input as the type information is retained.

<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <element xsi:type="FirstType"/>
</root>

Currently, Jackson discards the type information and neither processes it by resolving against XmlSeeAlso if it was available. If the same object was marshalled to JSON, it would result in the following JSON:

{"element": {}}

which cannot be marshalled to an object and where the necessary information to unmarshall this object is neither retained.

Suggested solution

Jackson should introduce a pseudo-attribute to retain type information to support the same form of type discovery. The created JSON should add the JAXB-type within an annotation as in:

{"element": {"@type": "FirstType"}}

where the client should process this information when unmarshalling to recover the instance type.

I tried an implementation of such support, without knowing if this is the most efficient implementation. Also, I discovered that my solution created a null instance for an empty element object as in the example what I worked around. If there is a better solution to that corner-case, I'd appreciate any feedback. I am happy to offer my time to integrate this into the official JAXB support if this is a desired extension.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions