Ignore invalid objects in JSON Array/Collection with Jackson
<p>I'm using Jackson to deserialize network responses containing arrays of custom objects.
I use a Builder to instantiate those objects, which is in charge of making sure that the object is valid (required fieds are present, etc..) and throws an IllegalStateException when trying to build an invalid object.</p>

<p>When I deserialize arrays of these objects, I would like to be able to ignore invalid ones, and still return an array containing the valid ones, instead of dropping all of them.
Ideally, I would like to have a callback or some kind of error handler so that I can do custom logging when that happens too.</p>

<p>Here is a short example:</p>

<pre><code>// Deserializing list with invalid object
String json = "[{\"id\":\"123\"}, {}, {\"id\":\"125\"}]";
ObjectMapper mapper = new ObjectMapper();
List&lt;MyObject&gt; objects = mapper.readValue(json, new TypeReference&lt;List&lt;MyObject&gt;&gt;() {});
// This will throw IllegalArgumentException
// How can I make this return [MyObject{id=123}, [MyObject{id=125}] instead?

// Custom Object
@JsonDeserialize(builder = MyObject.Builder.class)
public class MyObject {
  @NonNull
  private final String id;

  private MyObject(@NonNull String id) {
    this.id = id;
  }

 @NonNull
 public String getId() {
    return id;
  }

  public static class Builder {
    private String id;

    @JsonProperty("id")
    public Builder setId(@NonNull String id) {
      this.id = id;
      return this;
    }

    public MyObject build() {
      if (id == null) {
        throw new IllegalStateException("id is null");
      }
      return new MyObject(id);
    }
  }
}
</code></pre>