jackson-module-jaxb-annotations icon indicating copy to clipboard operation
jackson-module-jaxb-annotations copied to clipboard

Jackson can't handle @XmlElements and @XmlElementWrapper correctly

Open splatch opened this issue 11 years ago • 8 comments

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);
    }

}```

splatch avatar Nov 07 '14 18:11 splatch

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.

cowtowncoder avatar Nov 07 '14 19:11 cowtowncoder

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

splatch avatar Nov 10 '14 11:11 splatch

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:

  1. Output that serialization produces is not what user thinks (but deserialization of actual serialization would work) and
  2. Deserialization does not work, although input is what Jackson serialization produces

cowtowncoder avatar Nov 10 '14 23:11 cowtowncoder

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.

splatch avatar Nov 11 '14 07:11 splatch

Right, existing hard-coded XML structures are tricky.

cowtowncoder avatar Nov 11 '14 19:11 cowtowncoder

Any news here?

mkopylec avatar Jul 16 '17 13:07 mkopylec

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.

shevek avatar Aug 16 '17 23:08 shevek

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

cowtowncoder avatar May 19 '18 17:05 cowtowncoder