Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// Deserializing list with invalid object
String json = "[{\"id\":\"123\"}, {}, {\"id\":\"125\"}]";
ObjectMapper mapper = new ObjectMapper();
List<MyObject> objects = mapper.readValue(json, new TypeReference<List<MyObject>>() {});
// 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);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.