5

In the case of duplicates, ObjectMapper by default only keeps one of the elements as key. Namely, the last element will always be the one stored. For example:

{ "name" : "xyz", "name" : "abc" }

If we use ObjectMapper readTree on the above tree, it will drill down to:

--> ObjectMapper.readTree(<json-input>)

    --> ObjectMapper._readMapAndClose

        --> BaseNodeDeseralizer.deserializeObject

            --> JsonNode old = node.replace(key, value);    // replaces a key in case of duplicates

The above will add "name":"xyz" to the tree and then on the second run, replace it with "name":"abc".

There are configuration options to cause ObjectMapper to fail/spit error in case of duplicates. For Example:

mapper.enable(MapperFeature.IGNORE_DUPLICATE_MODULE_REGISTRATIONS);
mapper.enable(JsonParser.Feature.STRICT_DUPLICATE_DETECTION);
mapper.enable(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY);

But I couldn't find a configuration option to ALLOW duplicates.

I have already attempted Overriding the deserializeObject method to not do a replace. But this seems unwise, since my overriden method will "fall away" from the library's method and I won't be able to pick up future updates/fixes.

My question is: Is ObjectMapper allowed to keep duplicate keys (given the data structure it uses)? If so, how?

neid
  • 315
  • 1
  • 4
  • 12

0 Answers0