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.

`XmlMapper`/`UntypedObjectDeserializer` swallows duplicated elements in XML documents

See original GitHub issue

Hello guys.

I think I have already seen this issue around, but, for UntypedObjectDeserializer the solution maybe a bit different.

Entity

<?xml version="1.0" encoding="UTF-8"?>
<person>
   <name>John</name>
   <parent>Jose</parent>
   <parent>Maria</parent>
   <dogs>
      <count>3</count>
      <dog>
         <name>Spike</name>
         <age>12</age>
      </dog>
      <dog>
         <name>Brutus</name>
         <age>9</age>
      </dog>
      <dog>
         <name>Bob</name>
         <age>14</age>
      </dog>
   </dogs>
</person>

Code

new XmlMapper().readValue(xml, Object.class);       

Output

{
  "name" : "John",
  "parent" : "Maria",
  "dogs" : {
    "count" : "3",
    "dog" : {
      "name" : "Bob",
      "age" : "14"
    }
  }
}

Problem

Duplicated elements in the entity get swallowed by current UntypedObjectDeserializer implementation.

I can’t use Typed Objects. In my use case, I don’t have any typed objects, because I don’t know how objects are sent to me.

Possible Solution

While creating the Map for the data, check if there are duplicated keys, and start an Array, with this approach, the output would be:

{
  "name" : "John",
  "parent" : [ "Jose", "Maria" ],
  "dogs" : {
    "count" : "3",
    "dog" : [ {
      "name" : "Spike",
      "age" : "12"
    }, {
      "name" : "Brutus",
      "age" : "9"
    }, {
      "name" : "Bob",
      "age" : "14"
    } ]
  }
}

How to Reproduce

Gist

Artifacts:

  • jackson-core
  • jackson-databind
  • jackson-dataformat-xml

Version:

  • 2.7.4

Conclusion

The gist implements the solution using an extended version of the UntypedObjectDeserializer.

If not the default behavior, what about creating a new DeserializationFeature to enable this(default or not)?

Should you guys like/aprove this solution, I can always fork the project and submit a pull request with the full solution as a feature or default behavior.

Thanks! Jp

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:8
  • Comments:31 (11 by maintainers)

github_iconTop GitHub Comments

7reactions
cowtowncodercommented, May 23, 2020

I ended up implementing suggestion, so that repeated elements result in automatic “upgrade” into List. This is done automatically when reading xml content.

Will be in 2.12(.0).

1reaction
ViniciusArnholdcommented, Sep 11, 2019

@cowtowncoder Is this (Handling duplicate elements natively) targeted for 3.0?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jackson: XML to Map with List deserialization - Stack Overflow
In a nutshell, duplicated elements in the XML get swallowed by the current UntypedObjectDeserializer implementation. Fortunately, the author (João Paulo ...
Read more >
How To Allow Duplicate Keys With Jackson While ... - ADocLib
When encountering duplicate keys for JSON Objects should an exception be ... XmlMapper / UntypedObjectDeserializer swallows duplicated elements in XML ...
Read more >
com.fasterxml.jackson.databind.ObjectMapper Maven / Gradle ...
Note on caching: root-level deserializers are always cached, and accessed * using ... object used for determining whether given property element * (method, ......
Read more >
Integration - Spring
This part of the reference documentation covers Spring ... that can read and write XML by using Jackson XML extension's XmlMapper .
Read more >
users@jersey.java.net: by subject - Software Download
[Jersey] - consume json & automatic serialization to custom object ... [Jersey] 500 Internal server error while using xml mapper.
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