title: Custom deserialization of List using Jackson
body: <p>I am trying to write a custom deserializer in order to trim down a big set of data I receive from somewhere else. I return a List of custom objects from the deserializer. </p>
  
  <p>My question is, how do I do that, if this is my custom deserializer :</p>
  
  <pre><code>public class MyCustomDeserializer extends JsonDeserializer&lt;List&lt;CustomClass&gt;&gt; { ... }
  </code></pre>
  
  <p>I certainly can't do this :</p>
  
  <pre><code>final SimpleModule module = new SimpleModule();
  module.addDeserializer(List&lt;CustomClass&gt;.class, new MyCustomDeserializer());
  </code></pre>
  
  <p>Will something like this work ?</p>
  
  <pre><code>final List&lt;CustomClass&gt; response = Arrays.asList(objectMapper.readValue(stringBean, CustomClass[].class));
  </code></pre>
  
  <p>If this indeed works, I find it a bit confusing and "dangerous" ? Isn't the deserialization done inside the asList method invocation ? So it basically maps a List to an array[] ?</p>
  
  <p>I learned about TypeReference so I can probably use that like so :</p>
  
  <pre><code>objectMapper.readValue(stringBean, new TypeReference&lt;List&lt;CustomClass&gt;&gt;(){});
  </code></pre>
  
  <p>but I heard it is slower.</p>
  
  <p>I also don't want to create a container for the list, and return that in the deserialization because that means it will be wrapped in another json object, and I simply want my endpoint to produce something like :</p>
  
  <pre><code>[{object1}, {object2}]
  
  // instead of
  
  {"Output" : [{object1}, {object2}]}
  </code></pre>
  
  <p><strong>EDIT:</strong></p>
  
  <p>It seems that I have misinterpreted how jackson is using my deserializer in both cases :</p>
  
  <pre><code>final List&lt;CustomClass&gt; response = Arrays.asList(objectMapper.readValue(stringBean, CustomClass[].class));
  // or
  objectMapper.readValue(stringBean, new TypeReference&lt;List&lt;CustomClass&gt;&gt;(){});
  </code></pre>
  
  <p>It looks like the deserializer is called twice, once for each object in the array. I thought that the entire array would be considered as a whole. To clear the confusion, here is what I mean:</p>
  
  <p>The json I receive and try to deserialize looks like so :</p>
  
  <pre><code>[
    {
      "Data" : {
        "id" : "someId",
        "otherThing" : "someOtherThing"
      },
      "Message" : "OK"
    },
    {
      "Data" : null,
      "Message" : "Object not found for id blabla"
    }
  ]
  </code></pre>
  
  <p>and so I though this is what I would have inside my deserializer, but as I said before it seems that i actually get each "entry" from that array and call it multiple times.</p>