Do not serialize null values – jackson objectMapper (@JsonInclude)

Given a list of user defined objects or POJO and object may contains the null values. When we are converting list of objects to JSON, we do not want to serialize the null values.  We will use the JsonInclude annotation Jackson’s ObjectMapper to block serialization of nulls. We will create couple of objects, on which we will perform following operations

  • Given a list of Person objects.
    • Serialize the list of Person objects (default serialization).
    • Fields having nulls value will be serialized.
  • Given a list of Employee objects.
    • Employee class contains @JsonInclude annotation (which protects the null field serialization)
      • @JsonInclude(JsonInclude.Include.NON_NULL)
    • 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 null values not serialized except firstName field.

Jackson ObjectMapper Maven Dependencies

<dependency>
	<groupId>com.fasterxml.jackson.core</groupId> 
	<artifactId>jackson-databind</artifactId>
	<version>2.7.1</version> 
</dependency>

Program: Serialize non null properties of POJO or object

Person Class: The person class is as shown below

package org.learn.jackson;

public class Person {
	public String firstName;
	public String lastName;
	public int age;
	
	public Person() {		
	}
	
	public Person(String firstName, String lastName,
				int age) {
		this.firstName = firstName;
		this.lastName = lastName;
		this.age = age;
	}	
}

Employee Class: The Employee class is as shown below.

  • Do not serialize null values of employee objects (except firstName data member)
package org.learn.jackson;

import com.fasterxml.jackson.annotation.JsonInclude;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class Employee {
	
	@JsonInclude(JsonInclude.Include.ALWAYS)
	public String firstName;
	public String lastName;
	public String contact;
	public int age;
	
	public Employee() {		
	}
	
	public Employee(String firstName, String lastName,String contact,
				int age) {
		this.firstName = firstName;
		this.lastName = lastName;
		this.contact = contact;
		this.age = age;
	}	
}

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 null values of person objects
  2. Converting list of Employee objects to json
    • Do not serialize the null values of Employee objects (except firstName).
package org.learn.jackson;

import java.io.IOException;
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),
				  	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,"0012365412",18),
			  	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 null properties of POJO or object

1. Convert List of person object to JSON (Default serialization) :
[ {
  "firstName" : "Mike",
  "lastName" : "harvey",
  "age" : 34
}, {
  "firstName" : null,
  "lastName" : null,
  "age" : 0
} ]
2. Convert List of employee object to JSON (using @JsonInclude):
[ {
  "firstName" : "Steve",
  "contact" : "0012365412",
  "age" : 18
}, {
  "firstName" : null,
  "age" : 0
} ]
Scroll to Top