Skip to content
This repository has been archived by the owner on Jan 22, 2019. It is now read-only.

Jackson can't handle @XmlElements and @XmlElementWrapper correctly #34

Open
splatch opened this issue Nov 7, 2014 · 8 comments
Open

Comments

@splatch
Copy link
Contributor

splatch commented Nov 7, 2014

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

}```
@cowtowncoder
Copy link
Member

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.

@splatch
Copy link
Contributor Author

splatch commented Nov 10, 2014

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

@cowtowncoder
Copy link
Member

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

@splatch
Copy link
Contributor Author

splatch commented Nov 11, 2014

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.

@cowtowncoder
Copy link
Member

Right, existing hard-coded XML structures are tricky.

@mkopylec
Copy link

Any news here?

@shevek
Copy link

shevek commented Aug 16, 2017

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.

@cowtowncoder
Copy link
Member

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants