Jackson can't handle @XmlElements and @XmlElementWrapper correctly
Code written below works perfectly with plain JAXB but causes issues with Jackson. I found no way to handle similar structure by Fasterxml - wrapper + child elements having various names.
@XmlRootElement(name = "foo")
@XmlAccessorType(XmlAccessType.FIELD)
public class XmlFail {
@XmlElementWrapper(name = "items")
@XmlElements({
@XmlElement(name = "double", type = Double.class),
@XmlElement(name = "integer", type = Integer.class)
})
private List<Number> items;
public static void main(String[] args) throws Exception {
JAXBContext context = JAXBContext.newInstance(XmlFail.class);
String xml =
"<foo>" +
"<items>" +
"<double>2.0</double>" +
"<integer>1</integer>" +
"</items>" +
"</foo>";
XmlFail unmarshal = (XmlFail) context.createUnmarshaller().unmarshal(new ByteArrayInputStream(xml.getBytes()));
System.out.println(unmarshal.getClass() + " " + unmarshal.items);
}
}```
Hmmh. Odd. That should be something that could be supported, at least piece-wise.
Wrapper is supported (and there is a Jackson-specific alternative annotation); and @XmlElements could be considered to work same as @JsonTypeInfo and related subtype/id mappings.
One thing to check, first, would be to see what Jackson XML produces: that is, is there structural difference, or is something just being ignored.
I'm trying to read similar structure - I am not considered yet by writing, however it's get hard to proceed with some of XML weirdness (from JSON point of view).
I was trying to get this done with annotations:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include =JsonTypeInfo.As.WRAPPER_OBJECT)
@JsonSubTypes({
@JsonSubTypes.Type(name = "double", value = Double.class),
@JsonSubTypes.Type(name = "integer" , value = Integer.class)
})
@JacksonXmlElementWrapper(localName = "items")
private List<Number> checks;
I am unable to get this kind of construction working with any type - when I try to map complex structure. I think issue comes from bad state of parser in given place:
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
Hmmh. This could be related to types in question; while it should definitely work, handling of primitive/wrapper types may cause some wrinkles.
One thing to try out is to see what XML looks like when serializing this POJO. Guarantee that Jackson module gives is that it should be able to read anything it writes; so problems fall into two categories:
- Output that serialization produces is not what user thinks (but deserialization of actual serialization would work) and
- Deserialization does not work, although input is what Jackson serialization produces
In my case I am limited only to reading cause I am integrating with 3rd party system which produces XML without any schema and shape of output is hard to cover with JAXB. I have found in #125 that guilty of weirdness is sub typing and it does affect @JacksonXmlElementWrapper.
Right, existing hard-coded XML structures are tricky.
Any news here?
I think this is affecting me too - I either get this issue, or I get an issue where it won't ignore whitespace within an XmlElementWrapper element.
Will need to be moved to
https://github.com/FasterXML/jackson-modules-base/issues
as this repository is deprecated. Not sure what would be the best way as github has no transfer for issues, and I would like to get good updated description of the issue (which remains).