Given an list of user defined objects or POJO and object may contains the empty values (e.g. null values or collection of size=0), so while converting list of objects to json we do not want to serialize the empty values. We have already discussed about not serializing empty values using setSerializationInclusion. We will use the JsonInclude annotation Jackson’s ObjectMapper to block serialization of empty values. We will perform following operations on Person and Employee objects.
- Given a list of Person objects.
- Serialize the list of Person objects (default serialization).
- Fields having empty values will be serialized.
- Given a list of Employee objects.
- Employee class contains @JsonInclude annotation (which protects the empty field serialization)
- @JsonInclude(JsonInclude.Include.NON_EMPTY)
- Data member firstName of Employee class @JsonInclude
- Which ensures firstName always get serialized irrespective of its value (null or non null)
- @JsonInclude(JsonInclude.Include.ALWAYS)
- During serializations empty values not serialized except firstName field.
- Employee class contains @JsonInclude annotation (which protects the empty field serialization)
Jackson ObjectMapper Maven Dependencies
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.7.1</version> </dependency>
Models used for object to JSON conversion(jackson objectmapper)
Person Class: The person class is as follows
package org.learn.jackson; import java.util.ArrayList; import java.util.List; public class Person { public String firstName; public String lastName; public int age; public List vehicleList; public Person() { vehicleList = new ArrayList<>(); } public Person(String firstName, String lastName, int age, List vehicleList) { this.firstName = firstName; this.lastName = lastName; this.age = age; this.vehicleList = vehicleList; } }
Employee Class: The Employee class is as shown below.
- Do not serialize empty values of employee objects (except firstName data member)
ppackage org.learn.jackson; import java.util.ArrayList; import java.util.List; import com.fasterxml.jackson.annotation.JsonInclude; @JsonInclude(JsonInclude.Include.NON_EMPTY) public class Employee { @JsonInclude(JsonInclude.Include.ALWAYS) public String firstName; public String lastName; public int age; public List vehicleList; public Employee() { vehicleList = new ArrayList<>(); } public Employee(String firstName, String lastName, int age, List vehicleList) { this.firstName = firstName; this.lastName = lastName; this.age = age; this.vehicleList = vehicleList; } }
Program – serialize non empty values using Jackson objectmapper
JSONListConverter Class: We are performing couple of conversion from list of person objects to json.
- Converting list of person objects to json (Default serialization)
- Serializing the empty values of person objects
- Converting list of Employee objects to json
- Do not serialize the empty values of Employee objects (except firstName).
package org.learn.jackson; import java.io.IOException; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; public class JSONListConverter { public static void main( String[] args ) throws IOException { ObjectMapper objectMapper = new ObjectMapper(); //Set pretty printing of json objectMapper.enable(SerializationFeature.INDENT_OUTPUT); //List of person objects which will be converted to JSON List personList = Stream.of( new Person("Mike", "harvey", 34,Arrays.asList("bentley","ferrari")), new Person() ) .collect(Collectors.toList()); //1. Convert List Person to JSON (Serializing null values) String arrayToJson = objectMapper.writeValueAsString(personList); System.out.println("1. Convert List of person object to JSON (Default serialization) :"); System.out.println(arrayToJson); //List of employee objects List employeeList = Stream.of( new Employee("Steve", null,18, Arrays.asList("bentley","ferrari")), new Employee() ) .collect(Collectors.toList()); arrayToJson = objectMapper.writeValueAsString(employeeList); System.out.println("2. Convert List of employee object to JSON (using @JsonInclude):"); System.out.println(arrayToJson); } }
Output – serialize non empty values using Jackson objectmapper
1. Convert List of person object to JSON (Default serialization) : [ { "firstName" : "Mike", "lastName" : "harvey", "age" : 34, "vehicleList" : [ "bentley", "ferrari" ] }, { "firstName" : null, "lastName" : null, "age" : 0, "vehicleList" : [ ] } ] 2. Convert List of employee object to JSON (using @JsonInclude): [ { "firstName" : "Steve", "age" : 18, "vehicleList" : [ "bentley", "ferrari" ] }, { "firstName" : null, "age" : 0 } ]