This tutorial will show how to ignore certain fields when serializing an object to JSON using Jackson.
This is very useful when the Jackson defaults aren’t enough and we need to control exactly what gets serialized to JSON. There are several ways to ignore properties.
- Ignore Fields at the Class Level
- Ignore Field at the Field Level
- Ignore all Fields by Type
Dependencies
Let’s first add the following dependencies to the pom.xml:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
This dependency will also transitively add the following libraries to the classpath:
- jackson-annotations-2.9.8.jar
- jackson-core-2.9.8.jar
- jackson-databind-2.9.8.jar
Always use the latest versions on the Maven central repository for Jackson databind.
Let's demonstrate how to ignore certain fields when serializing an object to JSON using Jackson with examples.
1. Ignore Fields at the Class Level
We can ignore specific fields at the class level, using the @JsonIgnoreProperties annotation and specifying the fields:
package net.javaguides.jackson.ignore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(value = {
"id",
"firstName"
})
public class CustomerDTO {
private final String id;
private final String firstName;
private final String lastName;
public CustomerDTO(String id, String firstName, String lastName) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
public String getId() {
return id;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}
Let's test above code with a main() method, note that after the object is written to JSON, the field is indeed not part of the output:
package net.javaguides.jackson.ignore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class IgnoreFieldTest {
public static void main(String[] args) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
CustomerDTO dtoObject = new CustomerDTO("CUST100", "Tony", "Stark");
String dtoAsString = mapper.writeValueAsString(dtoObject);
System.out.println(dtoAsString);
}
}
Output:
{"lastName":"Stark"}
Note that we have ignored two fields "id" and "firstName" of CustomerDTO and printed only "lastName".
2. Ignore Field at the Field Level
We can also ignore a field directly via the @JsonIgnore annotation directly on the field:
package net.javaguides.jackson.ignore;
import com.fasterxml.jackson.annotation.JsonIgnore;
public class CustomerDTO {
@JsonIgnore
private final String id;
@JsonIgnore
private final String firstName;
private final String lastName;
public CustomerDTO(String id, String firstName, String lastName) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
public String getId() {
return id;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}
Let's test above code with a main() method, note that after the object is written to JSON, the field is indeed not part of the output:
package net.javaguides.jackson.ignore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class IgnoreFieldTest {
public static void main(String[] args) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
CustomerDTO dtoObject = new CustomerDTO("CUST100", "Tony", "Stark");
String dtoAsString = mapper.writeValueAsString(dtoObject);
System.out.println(dtoAsString);
}
}
Output:
{"lastName":"Stark"}
Note that we have ignored two fields "id" and "firstName" of CustomerDTO using @JsonIgnore annotation and printed only "lastName".
3. Ignore all Fields by Type
We can also ignore all fields of a specified type, using the @JsonIgnoreType annotation. If we control the type, then we can annotate the class directly:
@JsonIgnoreType
public static class Name {
public String firstName;
public String lastName;
public Name(String firstName, String lastName) {
super();
this.firstName = firstName;
this.lastName = lastName;
}
}
Here is the complete code:
package net.javaguides.jackson.ignore;
import com.fasterxml.jackson.annotation.JsonIgnoreType;
public class UserDTO {
public int id;
public Name name;
public UserDTO(int id, Name name) {
super();
this.id = id;
this.name = name;
}
@JsonIgnoreType
public static class Name {
public String firstName;
public String lastName;
public Name(String firstName, String lastName) {
super();
this.firstName = firstName;
this.lastName = lastName;
}
}
}
Let's test above code with a main() method, note that after the object is written to JSON, the field is indeed not part of the output:
package net.javaguides.jackson.ignore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonIgnoreTypeTest {
public static void main(String[] args) throws JsonProcessingException {
UserDTO.Name name = new UserDTO.Name("John", "Doe");
UserDTO user = new UserDTO(1, name);
String result = new ObjectMapper()
.writeValueAsString(user);
System.out.println(result);
}
}
Output:
{"id":1}
Note that the Name class field is ignored by using @JsonIgnoreType annotation.
Related Articles
- Change Field Name in JSON using Jackson (popular)
Comments
Post a Comment
Leave Comment