1. Overview
In this tutorial, we'll look at how to handle unknown fields during deserialization using Jackson. In other words, we have a field in the JSON string, but there is no matching field in the Java class.
2. Default Behavior
Let's start with the default behavior.
We have the Person class:
public class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Here Person has two fields, name and age.
When we try to deserialize the JSON string which includes weight field, it fails. Because weight is not present in Java object:
@Test(expected = JsonProcessingException.class)
public void shouldNotDeserialize_WithUnknown() throws IOException {
final String json = "{\"age\":12,\"weight\":99}";
objectMapper.readValue(json, Person.class);
}
Note that we also have a field missing in the JSON string - name. However, it doesn't cause a problem:
@Test
public void shouldDeserialize_WithMissing() throws IOException {
final String json = "{\"age\":12}";
Person deserialized = objectMapper.readValue(json, Person.class);
assertThat(deserialized.getName()).isNull();
assertThat(deserialized.getAge()).isEqualTo(12);
}
Next, we'll investigate different solutions to this problem.
3. Ignore unknown values with @JsonIgnoreProperties on the Class
We can ignore some fields using @JsonIgnoreProperties on the class level:
@JsonIgnoreProperties(ignoreUnknown = true)
public class PersonWithControl {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Here, we're adding @JsonIgnoreProperties to the class and setting its ignoreUnknown attribute to true.
Hence, when we try to deserialize with @JsonIgnoreProperties in place, unknown weight field doesn't cause an exception.
@Test
public void shouldDeserialize_WithUnknown_ViaClass() throws IOException {
final String json = "{\"age\":12,\"weight\":99}";
PersonWithControl deserialized = objectMapper.readValue(json, PersonWithControl.class);
assertThat(deserialized.getName()).isNull();
assertThat(deserialized.getAge()).isEqualTo(12);
}
4. Ignore unknown values with FAIL_ON_UNKNOWN_PROPERTIES on the ObjectMapper
Now, let's look at how we can ignore unknown fields on ObjectMapper level.
Jackson has DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES feature to decide how to handle unknown properties. It is set on ObjectMapper and affects all deserialization operations.
@Test
public void shouldDeserialize_WithUnknown_ViaReader() throws IOException {
final String json = "{\"age\":12,\"weight\":99}";
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Person deserialized = objectMapper.readValue(json, Person.class);
assertThat(deserialized.getName()).isNull();
assertThat(deserialized.getAge()).isEqualTo(12);
}
Here, unknown weight field doesn't also cause an exception.
5. Summary
In this tutorial, we've investigated how to ignore unknown properties during serialization using Jackson.
Check out the source code for the examples in this article over on Github.