- Given a list of user defined objects or POJO and object may contains the empty values (e.g. null values or collection of size=0).
- When, we are converting list of objects to JSON, we do not want to serialize the empty values.
- We will use the setSerializationInclusion feature of jackson’s ObjectMapper to block serialization of empty values.
- We will create Person class and we will perform the following operations with Person class
- Convert List of Person objects to JSON string (Default Serialization)
- Serializes the empty values
- Convert List of Person objects to JSON string
- Do not serializes the empty values
- Convert List of Person objects to JSON string (Default Serialization)
- We have already discussed about serialization of non-null values (while converting object to/from JSON).
Jackson ObjectMapper Maven Dependencies
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.7.1</version> </dependency>
Program: Serialize non empty properties of POJO or object
Person Class: The person class is as follows:
package org.learn.jackson; import java.util.List; public class Person { public String firstName; public String lastName; public int age; public List vehicleList; public Person() { } public Person(String firstName, String lastName, int age) { this.firstName = firstName; this.lastName = lastName; this.age = age; } public Person(String firstName, String lastName, int age, List vehicleList) { this.firstName = firstName; this.lastName = lastName; this.age = age; this.vehicleList = vehicleList; } }
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 person objects to json
- Do not serialize the empty values of person objects.
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.annotation.JsonInclude.Include; 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 (Serializing empty values) :"); System.out.println(arrayToJson); //ObjectMapper for not serializing null values objectMapper = new ObjectMapper(); //Set pretty printing of json objectMapper.enable(SerializationFeature.INDENT_OUTPUT); //Setting the serializing feature: do not serialize null values objectMapper.setSerializationInclusion(Include.NON_EMPTY); arrayToJson = objectMapper.writeValueAsString(personList); System.out.println("2. Convert List of person object to JSON (Serializing non empty values):"); System.out.println(arrayToJson); } }
Output: Serialize non empty values of user defined object
1. Convert List of person object to JSON (Serializing empty values) : [ { "firstName" : "Mike", "lastName" : "harvey", "age" : 34, "vehicleList" : [ "bentley", "ferrari" ] }, { "firstName" : null, "lastName" : null, "age" : 0, "vehicleList" : null } ] 2. Convert List of person object to JSON (Serializing non empty values): [ { "firstName" : "Mike", "lastName" : "harvey", "age" : 34, "vehicleList" : [ "bentley", "ferrari" ] }, { "age" : 0 } ]