Site icon

Do not serialize empty values of object – Jackson ObjectMapper (example)

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.

  1. Converting list of person objects to json (Default serialization)
    • Serializing the empty values of person objects
  2. 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
} ]
Exit mobile version