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