title: Jackson @JsonAnySetter ignores values of duplicate key when used with Jackson ObjectMapper treeToValue method
body: <p>I am using the Jackson library to deserialize JSON. In the JSON I have a few custom fields whose values can be anything, so I am trying to use the <code>@JsonAnySetter</code> and <code>@JsonAnyGetter</code> to obtain the values. The fields and values within the JSON can be duplicated and I would like to obtain everything from the JSON and store it within the map. However, the direct Jackson derealization is storing the last value if there are duplicates in the key.</p>
  <p>I have a large JSON file that has many events. I am reading the file event-by-event so that the whole JSON file is not stored within memory. After reading a single event, I check the type of event, based on which I assign it to a different POJO. Following is my sample JSON file consisting of 2 events.</p>
  <pre><code>[
    {
      &quot;isA&quot;: &quot;Type1&quot;,
      &quot;name&quot;: &quot;Test&quot;,
      &quot;foo&quot;: &quot;val1&quot;,
      &quot;foo&quot;: &quot;val2&quot;,
      &quot;bar&quot;: &quot;val3&quot;,
      &quot;foo&quot;: {
        &quot;myField&quot;: &quot;Value1&quot;,
        &quot;myField&quot;: &quot;value2&quot;
      }
    },
    {
      &quot;isA&quot;: &quot;Type2&quot;,
      &quot;name&quot;: &quot;Test1&quot;,
      &quot;foo&quot;: &quot;val1&quot;,
      &quot;foo&quot;: &quot;val2&quot;,
      &quot;bar&quot;: &quot;val3&quot;,
      &quot;foo&quot;: {
        &quot;myField&quot;: &quot;Value1&quot;,
        &quot;myField&quot;: &quot;value2&quot;
      }
    }
  ]
  </code></pre>
  <p>Following is the class that is used for deserialization: (I have many fields which are mapped directly during the deserialization and working correctly so omitted for simplicity). This is the class for the <code>type1</code> event if it's <code>type2</code></p>
  <pre><code>@JsonInclude(JsonInclude.Include.NON_NULL)
  @Data
  @NoArgsConstructor
  @AllArgsConstructor
  @Builder
  public class Type1
  {
      private String name;
      private Map&lt;String, Object&gt; userExtensions;
     
      @JsonAnyGetter
      public Map&lt;String, Object&gt; getUserExtensions() {
          return userExtensions;
      }
      
      @JsonAnySetter
      public void setUserExtensions(String key, Object value) {
          System.out.println(&quot;KEY : &quot; + key + &quot; VALUES : &quot; + values);
      }
      
  }
  </code></pre>
  <p>As you can observe from above I have a <code>Map</code> field that is used to populate the extensions using the <code>JsonAnySetter</code>. I have a method which will be called for the fields which cannot be directly searlized by Jackson.</p>
  <p>I tried setting the <code>System.out</code> within the <code>@JsonAnySetter</code> method and I observed that Jackson does not get the duplicate field within this method:</p>
  <pre><code>  @JsonAnySetter
    public void setUserExtensions(String key, Object value) {
      System.out.println(&quot; Key : &quot; + key + &quot; Value : &quot; + value);
    }
  </code></pre>
  <p>I get only the last fields in this. For the above mentioned JSON I get only the last <code>foo</code> which has the value:</p>
  <pre><code>&quot;foo&quot; :{
    &quot;myField&quot; : &quot;Value1&quot;
    &quot;myField&quot; : &quot;value2&quot;
  }
  </code></pre>
  <p>The first 2 <code>foo</code> with <code>val1</code> and <code>val2</code> does not even print within this method.</p>
  <p>Following is my <code>Main</code> method which is actually the culprit as I am using the <code>objectMapper.treeToValue</code> of Jackson which does not support the duplicate fields.</p>
  <pre><code>public class Main
  {
    public static void main (String[]args)
    {
      //File where the JSON is stored
      InputStream jsonStream = Main.class.getClassLoader().getResourceAsStream(&quot;InputEPCISEvents.json&quot;);
      final JsonFactory jsonFactory = new JsonFactory();
      final JsonParser jsonParser = jsonFactory.createParser (jsonStream);
      jsonParser.setCodec (new ObjectMapper ());
      final ObjectMapper objectMapper = new ObjectMapper();
      
      // Loop until the end of the events file
      while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
          // Get the node
          final JsonNode jsonNode = jsonParser.readValueAsTree();
          // Get the eventType
          final String eventType = jsonNode.get(&quot;isA&quot;).toString().replaceAll(&quot;\&quot;&quot;, &quot;&quot;);
          
          switch (eventType) {
              case &quot;Type1&quot;:
                  final Type1 objInfo = objectMapper.treeToValue(jsonNode, Type1.class);
                  break;
              case &quot;Type2&quot;:
                  final Type2 objInfo = objectMapper.treeToValue(jsonNode, Type2.class);
                  break;
              default:
                  System.out.println(&quot;None of the event Matches&quot;);
                  break;
          }
          
      }
        
    }
  }
  </code></pre>
  <p>I wanted to know how can I make Jackson read the duplicate key values so that I can handle them within the <code>@JsonAnySetter</code> method.</p>
  <p>I wanted to know if there is a way to handle these scenarios directly using Jackson or I need to build my own custom deserialization. If yes, then how can I build one for only one field within the class?</p>
  <p>PS: I tried many things available online but none worked hence posting the same. If found duplicate then I am really sorry.</p>
  <p>I noticed that most of the code sample on Stack Overflow read JSON with the Jackson <code>readValue</code> method, but in my case I have a large JSON file that has many events so I am splitting them event by event and using the Jackson <code>objectMapper.treeToValue</code> method to map it to my class. Within this class, I try to map the fields of each event with the respective class fields and if no match found then I am expecting them to be populated using the <code>@JsonAnySetter</code> method.</p>