jackson JAXB annotations - inheritance mapping at the root element

798 views
Skip to first unread message

Jakub Stransky

unread,
Apr 12, 2015, 6:58:41 PM4/12/15
to jackso...@googlegroups.com

I have following Jaxb annotated class hierarchy including inheritance at the document root element:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ParentClass", propOrder = {
        "parentField"
})
public class ParentClass{
    @XmlElement(name = "ParentField")
    protected String parentField;

  getters and setters here
}

ChildAClass:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ChildAClass", propOrder = {
        "childAfield"
})
public class ChildAClass extends ParentClass{

    @XmlElement(name = "ChildAfield")
    protected String childAfield;

  getters and setters here
}

ChildBClass:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ChildBClass", propOrder = {
        "childBfield"
})
public class ChildBClass extends ParentClass{

    @XmlElement(name = "ChildBfield")
    protected String childBfield;

  getters and setters here
}

That is pretty simple class hierarchy. Than I have simple test where I serialize a ChildAClass and try to deserialize to a ParentClass. I expect propper class to be deserialized back and upcasted to ParentClass. I suppose this is a valid use case.

Serialized document looks as follows, nothing fancy here.

{
  "ChildAfield": "child A field",
  "ParentField": "parent from child A"
}

But when I try to de-serialize :

    mapper = new ObjectMapper();
    JaxbAnnotationModule jaxbAnnotationModule = new JaxbAnnotationModule();
    mapper.registerModule(jaxbAnnotationModule);

    mapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
    mapper.configure(SerializationFeature.WRITE_ENUMS_USING_INDEX, false);
    mapper.setSerializationInclusion(JsonInclude.Include.NON_
ParentClass parentClass = mapper.readValue(new File(PATH_TO_FILE), ParentClass.class);

I am getting following exception:

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "ChildAfield" (class inheritance.model.ParentClass), not marked as ignorable (one known property: "ParentField"])
 at [Source: src/test/resources/testfiles/json/inheritance.json; line: 2, column: 19] (through reference chain: inheritance.model.ParentClass["ChildAfield"])
    at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:79)
    at com.fasterxml.jackson.databind.DeserializationContext.reportUnknownProperty( ...

I suppose that some type metadata are missing here for propper type inference and deserialization. I was looking to @JsonTypeInfo but I am restricted to JAXB. I tried @XmlElements jaxb annotation but wasn't able to make it work.

Can someone give a hint?

Thx

Jakub Stransky

unread,
Apr 12, 2015, 6:58:41 PM4/12/15
to jackso...@googlegroups.com

Tatu Saloranta

unread,
Apr 13, 2015, 2:49:33 PM4/13/15
to jackso...@googlegroups.com
I don't remember if this usage of `@XmlType` is recognized by JAXB annotations module or not -- if not, it would be matter of adding that support.
This is why it would make sense to try out usage of `@JsonTypeInfo` first, see that it works the way expected (as it should).

If @JsonTypeInfo works, @XmlType not, it'd make sense to report an issue at:

https://github.com/FasterXML/jackson-module-jaxb-annotations/issues

-+ Tatu +-


--
You received this message because you are subscribed to the Google Groups "jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jackson-user...@googlegroups.com.
To post to this group, send email to jackso...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages