1. Overview
In this article, we will discuss the purpose of the @JsonIgnoreProperties and @JsonIgnore Jackson annotations to ignore fields or properties during serialization or deserialization.
By default, Jackson serializes all the fields of a Java object to JSON. Similarly, all the JSON fields are deserialized to a Java object.
There are scenarios where you want to ignore certain properties during serialization or deserialization. To perform serialization or deserialization, we use the ObjectMapper class available in the Jackson library.
Let’s explore these annotations with examples.
2. Jackson @JsonIgnoreProperties
You can use this annotation to ignore the properties during serialization or deserialization. This annotation can be applied to both class and properties starting from 2.0.
2.1. @JsonIgnoreProperties – class level
In the below example, we applied the @JsonIgnoreProperties
annotation to the class Student
. We specified the properties to ignore as an array of strings in the annotation. @JsonIgnoreProperties({"dept", "name"})
– “dept” and “name” are property names to be ignored.
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; @JsonIgnoreProperties({"dept", "name"}) public class Student { public Student(long id, String name, String dept) { this.id = id; this.studentName = name; this.dept = dept; } public Student() { } private long id; @JsonProperty("name") private String studentName; private String dept; @Override public String toString() { return "Student [id=" + id + ", studentName=" + studentName + "]"; } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getStudentName() { return studentName; } public void setStudentName(String studentName) { this.studentName = studentName; } public String getDept() { return dept; } public void setDept(String dept) { this.dept = dept; } }
Let’s perform serialization of the Student
object and check Jackson ignored the properties “dept” and “name”.
In the below code, we have a student
object that has id
, name
and dept
properties. When we call writeValue
method of ObjectMapper
class to serialize the student
object to JSON, the resultant JSON contains only id
attribute.
@SpringBootApplication public class JacksonFieldNameApplication implements CommandLineRunner { public static void main(String[] args) { SpringApplication.run(JacksonFieldNameApplication.class, args); } @Override public void run(String... args) throws Exception { Student student = new Student(834567, "Michael Jackson", "IT"); ObjectMapper mapper = new ObjectMapper(); Writer writer = new OutputStreamWriter(System.err); mapper.writeValue(writer, student); } }
Similarly, the Jackson ignores those properties in the deserialization as well.
In the below code, the Jackson ignores the name
and dept
fields of JSON. So the resultant student
object has value for the id
property only.
@SpringBootApplication public class JacksonFieldNameApplication implements CommandLineRunner { public static void main(String[] args) { SpringApplication.run(JacksonFieldNameApplication.class, args); } @Override public void run(String... args) throws Exception { ObjectMapper mapper = new ObjectMapper(); String json = "{\"id\":834567,\"name\":\"Michael Jackson\", \"dept\":\"IT\"}"; System.out.println(mapper.readValue(json, Student.class)); } }
2.2. @JsonIgnoreProperties – field level
You can also apply the annotation @JsonIgnoreProperties
to a field in Java object.
In the below example, the Student
class contains a reference to another class Department
. Assume you want to ignore certain fields in the Department
class during serialization or deserialization.
public class Department { private String deptName; private long deptId; public String getDeptName() { return deptName; } public void setDeptName(String deptName) { this.deptName = deptName; } public long getDeptId() { return deptId; } public void setDeptId(long deptId) { this.deptId = deptId; } @Override public String toString() { return "Department [deptName=" + deptName + ", deptId=" + deptId + "]"; } }
The Department
class contains deptId
and deptName
. We specified @JsonIgnoreProperties
annotation to the field dept
in the Student
class. This annotation ignores the deptName
during serialization or deserialization.
public class Student { public Student(long id, String name, Department dept) { this.id = id; this.studentName = name; this.setDept(dept); } public Student() { } private long id; @JsonProperty("name") private String studentName; @JsonIgnoreProperties({"deptName"}) private Department dept; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getStudentName() { return studentName; } public void setStudentName(String studentName) { this.studentName = studentName; } public Department getDept() { return dept; } public void setDept(Department dept) { this.dept = dept; } @Override public String toString() { return "Student [id=" + id + ", studentName=" + studentName + ", dept=" + dept + "]"; } }
Let’s take deserialization to confirm this. The below JSON contains deptId
and deptName
fields. But Jackson ignores the deptName
during the deserialization. Thus, deptName
of dept
would be null.
@SpringBootApplication public class JacksonFieldNameApplication implements CommandLineRunner { public static void main(String[] args) { SpringApplication.run(JacksonFieldNameApplication.class, args); } @Override public void run(String... args) throws Exception { ObjectMapper mapper = new ObjectMapper(); String json = "{\"id\":834567,\"name\":\"Michael Jackson\", \"dept\":{\"deptId\":\"1\",\"deptName\":\"Maths\"}}"; System.out.println(mapper.readValue(json, Student.class)); } }
2.3. Jackson @JsonIgnoreProperties Ignoreunknown property
In some scenarios, the JSON might have fields that is not declared in the Java object.
The @JsonIgnoreProperties(ignoreUnknown = true)
annotation with property ignoreUnknown
ignores any unrecognized properties during deserialization. This doesn’t affect serialization.
The below Student
class contains only the id
property.
package com.tedblob.jackson.fieldname.models; public class Student { public Student(long id) { this.id = id; } public Student() { } private long id; public long getId() { return id; } public void setId(long id) { this.id = id; } @Override public String toString() { return "Student [id=" + id + "]"; } }
The below JSON contains id
, name
fields. Since Student
has only id
property, the writeValue
deserialization code would throw the following error.
Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field “name” (class com.tedblob.jackson.fieldname.models.Student), not marked as ignorable (one known property: “id”])
at [Source: (String)”{“id”:834567,”name”:”Michael Jackson”}”; line: 1, column: 22] (through reference chain: com.tedblob.jackson.fieldname.models.Student[“name”])
@SpringBootApplication public class JacksonFieldNameApplication implements CommandLineRunner { public static void main(String[] args) { SpringApplication.run(JacksonFieldNameApplication.class, args); } @Override public void run(String... args) throws Exception { ObjectMapper mapper = new ObjectMapper(); String json = "{\"id\":834567,\"name\":\"Michael Jackson\"}"; System.out.println(mapper.readValue(json, Student.class)); } }
Assume the property name
exists but there are no getters or setters to accept the value. This also throws the same UnrecognizedPropertyException
error.
public class Student { public Student(long id, String name) { this.id = id; this.name = name; } public Student() { } private long id; private String name; public long getId() { return id; } public void setId(long id) { this.id = id; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + "]"; } }
You can fix the above error by simply specifying the ignoreUnknown
property as below:
@JsonIgnoreProperties(ignoreUnknown = true) public class Student { public Student(long id) { this.id = id; } public Student() { } private long id; public long getId() { return id; } public void setId(long id) { this.id = id; } @Override public String toString() { return "Student [id=" + id + "]"; } }
Jackson ignores the unrecognized properties during the deserialization process from JSON to Java object and produces the following result:
Student [id=834567]
3. Jackson @JsonIgnore to ignore a specific field
To ignore a specific field, you can use the annotation @JsonIgnore
. You can apply this directly on the field to be ignored.
For example, the below code ignores the studentName
property of Java class using the @JsonIgnore annotation during serialization or deserialization.
import com.fasterxml.jackson.annotation.JsonIgnore; private long id; @JsonIgnore private String studentName;
The below JSON contains id
and studentName
fields. However, Jackson ignores the studentName
while converting the JSON to the student
object.
@Override public void run(String... args) throws Exception { ObjectMapper mapper = new ObjectMapper(); String json = "{\"id\":834567,\"studentName\":\"Michael Jackson\"}"; System.out.println(mapper.readValue(json, Student.class)); }
4. Conclusion
To sum up, we have seen the annotations @JsonIgnoreProperties and @JsonIgnore to ignore fields or properties in Jackson during the serialization or deserialization process.
Pingback: Jackson UnrecognizedPropertyException: Unrecognized field not marked as Ignorable - TedBlob