@JsonInclude annotation is used to exclude properties during object serialization. By default, all properties of a class are included during serialization. But by using the @JsonInclude annotation, we can exclude a few properties during serialization. This annotation can be applied at the field, method, or class levels.
@JsonInclude annotation provides us with different options. A few of them are listed below –
We have created a Person model class. In this class, one property is of java optional type. Please go through this post if you wish to learn how to use optional property with Jackson.
We will use the same class to understand these options.
import java.util.Optional;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
public class Person {
private int id;
private Optional<String> firstName;
private String lastName;
private Integer age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Optional<String> getFirstName() {
return firstName;
}
public void setFirstName(Optional<String> firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
// serializing example
public static void main(String[] args) throws JsonProcessingException {
Person person = new Person();
person.setId(1);
person.setFirstName(Optional.empty());
person.setLastName("");
person.setAge(null);
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new Jdk8Module());
System.out.println(mapper.writeValueAsString(person));// serializaing object
}
}
If we run the above program, then we will get the below output –
{"id":1,"firstName":null,"lastName":"","age":null}
Now, let’s see how @JsonInclude annotation can be used here in different situations.
Include.NON_NULL
Include.NON_NULL is used to ignore null properties in the class. After using this, only non-null values will be included in the serialization.
Using Include.NON_NULL
@JsonInclude(value = Include.NON_NULL)
public class Person {
//class
}
Output:
{"id":1,"firstName":null,"lastName":""}
age field value was null, and thus it wasn’t included in the resulting JSON. You might wonder why firstName is present in the output as it also seems to have a null value. It’s because firstName has Optional<String> as the data type, and to filter this out, we have to use Include.NON_ABSENT.
Include.NON_ABSENT
Include.NON_ABSENT will exclude the property whose value is-
- null
- or absent” value of a referential type ( Java optional or AtomicReference)
Include.NON_ABSENT is mainly used to work with “Optional”s (Java 8, Guava). And it seems that NON_ABSENT is a superset of NON_NULL because it also excludes the property with the null value.
Using Include.NON_ABSENT
@JsonInclude(value = Include.NON_ABSENT)
public class Person {
}
Output:
{"id":1,"lastName":""}
As we can see, age and firstName are not in the output. But lastName is still there with an empty value. Now, to remove the lastName, we would need to use Include.NON_EMPTY
Include.NON_EMPTY
Include.NON_EMPTY excludes null, absent, and properties with an empty value.
How to check for non-emptiness of properties?
- isEmpty() method is used to check whether Collections and Maps are empty or not
- Empty arrays are the ones with a length of 0
- And length() function is used to check the emptiness of strings
We can also say that the NON_EMPTY would also exclude properties excluded by NON_NULL and NON_ABSENT. So, NON_EMPTY becomes a superset that includes the functionalities of both NON_NULL and NON_ABSENT, plus some more.
Using Include.NON_EMPTY
@JsonInclude(value = Include.NON_EMPTY)
public class Person {
}
Output:
{"id":1}
All null, absent and empty value properties are ignored during serialization.
Include.ALWAYS
Include.ALWAYS is used to include all properties irrespective of the property’s value.
Using Include.ALWAYS
@JsonInclude(value = Include.ALWAYS)
public class Person {
}
Output:
{"id":1,"firstName":null,"lastName":"","age":null}
In this article, we understand how @JsonInclude works and learn a few of the options used with this annotation.
We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.
Other Jackson Annotations –
- @JsonIgnoreProperties and @JsonIgnore annotations
- @JsonPropertyOrder Jackson Annotation
- @JsonProperty Jackson Annotation
- @JsonRawValue Jackson Annotation
- @JsonValue Jackson Annotation
- @JsonAnyGetter Jackson Annotation
- @JsonAnySetter Jackson Annotation
- Java Optional with Jackson
- @JsonFormat Jackson Annotation