question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can not deserialize unwrapped list when `@JacksonXmlProperty` localName matches `@JacksonXmlRootElement` localName

See original GitHub issue

Please refer to http://stackoverflow.com/questions/20673153/using-jackson-to-deserialize-a-collection-of-children-with-the-same-type-as-the

I can not figure out how to deserialize a document like this:

<test id="0">
    <test id="0.1">
        <test id="0.1.1" />
    </test>
    <test id="0.2" />
    <test id="0.3">
        <test id="0.3.1" />
    </test>
</test>

Where each test element is deserialized into a simple POJO.

Here is the code I am dealing with.

public class UnitTest {

  @Test
  public void test() throws Exception {
    final ObjectMapper mapper = new XmlMapper();

    final XmlTest before =
        new XmlTest("0", Lists.newArrayList(new XmlTest("0.1", null),
            new XmlTest("0.2", Lists.newArrayList(new XmlTest("0.2.1", null)))));
    final String xml = mapper.writeValueAsString(before);
    System.out.println(xml);
    final XmlTest after = mapper.readValue(xml, XmlTest.class);
  }

  @JacksonXmlRootElement(localName = "test")
  public static class XmlTest {
    @JacksonXmlProperty(localName = "id", isAttribute = true)
    private String id;
    @JacksonXmlElementWrapper(useWrapping = false)
    @JacksonXmlProperty(localName = "test")
    private List<XmlTest> children;

    public XmlTest() {}

    public XmlTest(final String id, final List<XmlTest> children) {
      this.id = id;
      this.children = children;
    }
  }

}

My imported dependencies:

    compile group: 'com.fasterxml.jackson.dataformat', name: 'jackson-dataformat-xml', version: jacksonVersion
    compile group: 'com.fasterxml.jackson.jaxrs', name: 'jackson-jaxrs-xml-provider', version: jacksonVersion

    jacksonVersion=2.2.+

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Reactions:2
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
cowtowncodercommented, May 13, 2020

Initially was planning to fix for 2.12 only, but as per user request, backport in 2.11 for 2.11.1.

0reactions
cowtowncodercommented, May 12, 2020

I fixed #393, which is probably related, but unfortunately that did not yet resolve this issue. Hoping to have a look to see if I can fix it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - Can not deserialize unwrapped list when ...
I have found that changing the name of the root element in the XML document will result in successful deserialization. This is interesting ......
Read more >
Issue with order of inheritance type attribute - Google Groups
The deserialization fails when the type attribute is not the first attribute in the ... @JacksonXmlProperty(localName = ATTRIBUTE_TYPE, isAttribute = true)
Read more >
Solving the XML Problem with Jackson - Stackify
This can only adjust the Namespace and Local name – since the root element can never be serialized as an attribute. For example,...
Read more >
Jackson 2.12: improved XML module - cowtowncoder - Medium
Nested Lists in POJOs should work reliably (esp. unwrapped) ... to have edge cases for deserialization (serialization did not have issues); ...
Read more >
[Solved]-Getting Thumbnails from Google books api-Java
I find these nested Json files/strings are confusing, but I did get the above to work by reading another post here. Here is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found