Most JSON parsers will reject your input file out of hand, as duplicate keys at the same nesting level are not allowed (this is a de-facto standard). However, certain parsers will allow you to handle the duplicate in a variety of ways.
One way to handle this in Jackson, would be to map regular attributes into an entity class, then handle the potential duplicates via a @JsonAnySetter
.
public class Bag {
final transient Multimap<String, Object> multimap = LinkedListMultimap
.create();
// regular properties, constructors etc
@JsonAnySetter
public void add(final String key, final String value) {
multimap.put(key, value);
}
}
Note the use of a multimap: regular hash maps cannot contain duplicate keys, so a multimap is a requirement for a working solution. After deserializing the input file, all 'regular' JSON attributes will be mapped to their corresponding entity properties, whereas all duplicates will be stored in the map, and available for manual processing.
final List<Object> duplicatedValues = multimap.get(someKey);
Alternatively, you could create a custom deserializer, which will recieve all tokens (regardless of wether they are duplicates or not).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…