@JsonInclude(JsonInclude.Include.NON_DEFAULT) can be used to exclude the properties with POJO defaults. This is applied differently as follows:
- If @JsonInclude(JsonInclude.Include.NON_DEFAULT) is used on the class level then default values of the fields are excluded. This is done by creating an instance of POJO using zero-argument constructor, and comparing the property values, excluding the ones having default values e.g. default int value is 0, default String value is null and so on.
- if either @JsonInclude(JsonInclude.Include.NON_DEFAULT) is used on the property level or using:
ObjectMapper.setDefaultPropertyInclusion(JsonInclude.Include.NON_DEFAULT); In both cases :
- All values which are considered "empty" (as per NON_EMPTY last tutorial) are excluded
- Primitive/wrapper default values are excluded
- Date/time values that have milliseconds of `0L` are excluded
Example
Using NON_DEFAULT at class level
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
public class Employee {
private String name;
private String dept;
private Integer salary;
private boolean fullTime;
private List<String> phones;
private Date dateOfBirth;
.............
}
public class ExampleMain {
public static void main(String[] args) throws IOException {
Employee employee = new Employee();
employee.setName("Trish");
employee.setFullTime(false);
employee.setPhones(new ArrayList<>());
employee.setSalary(Integer.valueOf(0));
employee.setDateOfBirth(new Date(0));
ObjectMapper om = new ObjectMapper();
String jsonString = om.writeValueAsString(employee);
System.out.println(jsonString);
}
} {"name":"Trish","salary":0,"phones":[],"dateOfBirth":0}
As seen in above output only properties with default member values were excluded. Integer with 0 (primitive wrapper - salary) is not excluded, Date with 0 milliseconds is not excluded and also 'empty' collection (phones) is not excluded.
Using NON_DEFAULT at property level
public class Employee2 {
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
private String name;
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
private String dept;
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
private Integer salary;
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
private boolean fullTime;
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
private List<String> phones;
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
private Date dateOfBirth;
.............
}
public class ExampleMain2 {
public static void main(String[] args) throws IOException {
Employee2 employee = new Employee2();
employee.setName("Trish");
employee.setFullTime(false);
employee.setPhones(new ArrayList<>());
employee.setSalary(Integer.valueOf(0));
employee.setDateOfBirth(new Date(0));
ObjectMapper om = new ObjectMapper();
String jsonString = om.writeValueAsString(employee);
System.out.println(jsonString);
}
} {"name":"Trish"}
Using NON_DEFAULT globally
public class Employee3 {
private String name;
private String dept;
private Integer salary;
private boolean fullTime;
private List<String> phones;
private Date dateOfBirth;
.............
}
public class ExampleMain3 {
public static void main(String[] args) throws IOException {
Employee2 employee = new Employee2();
employee.setName("Trish");
employee.setFullTime(false);
employee.setPhones(new ArrayList<>());
employee.setSalary(Integer.valueOf(0));
employee.setDateOfBirth(new Date(0));
ObjectMapper om = new ObjectMapper();
om.setDefaultPropertyInclusion(JsonInclude.Include.NON_DEFAULT);
String jsonString = om.writeValueAsString(employee);
System.out.println(jsonString);
}
} {"name":"Trish"}
As seen above last two options produced the same results, i.e. 'empty' values, primitive/wrapper default values, and date with 0L milliseconds were also excluded.
Example ProjectDependencies and Technologies Used: - jackson-databind 2.9.6: General data-binding functionality for Jackson: works on core streaming API.
- JDK 10
- Maven 3.5.4
|