1. Overview

In this tutorial, we’ll look at serialization of null values. By default, Jackson serializes objects with all visible properties, but we can control which properties should be serialized including fields with null values.

2. Serialize Null Fields Fields/Properties

Firstly, let’s look at the default behavior.

With its default settings, Jackson serializes null-valued public fields. In other words, resulting JSON will include null fields.

We have PublicPerson class:

public class PublicPerson {

    public String name;
    public int age;

    public PublicPerson() {
    }

    public PublicPerson(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

When we serialize an instance of PublicPerson:

@Test
public void shouldSerialize_WithPublicFields_AndNullValues() throws JsonProcessingException {
    PublicPerson publicPerson = new PublicPerson(null, 21);

    String json = objectMapper.writeValueAsString(publicPerson);

    assertThat(json).isEqualTo("{\"name\":null,\"age\":21}");
}

Here, the name field which is null is in the resulting JSON string.

Now let’s switch to properties – fields with accessors.

We have the GetterPerson class:

public class GetterPerson {

    private String name;
    private int age;

    public GetterPerson() {
    }

    public GetterPerson(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }
}

Here, GetterPerson has two fields and public accessor methods.

When we serialize an instance of GetterPerson, it also serializes null properties – null returning getter methods:

@Test
public void shouldSerialize_WithGetters_AndNullValues() throws JsonProcessingException {
    GetterPerson getterPerson = new GetterPerson(null, 21);

    String json = objectMapper.writeValueAsString(getterPerson);

    assertThat(json).isEqualTo("{\"name\":null,\"age\":21}");
}

3. Ignore Null Values During Serialization

Now, let’s see how we can ignore null values during serialization.

3.1. Ignore with @JsonInclude on the Class

Firstly, we can annotate a class with @JsonInclude. This way we can notify Jackson how to handle null values for a specific class:

@Test
public void shouldSerialize_WithNonNullInclusion_OnClass() throws JsonProcessingException {

    @JsonInclude(Include.NON_NULL)
    class Car {

        private String name;
        private int age = 12;

        public String getName() {
            return name;
        }

        public int getAge() {
            return age;
        }
    }

    Car car = new Car();

    String json = objectMapper.writeValueAsString(car);

    assertThat(json).isEqualTo("{\"age\":12}");
}

Here, we’re stating @JsonInclude(Include.NON_NULL) to serialize only non-null properties. So the resulting JSON includes the age property but doesn’t include null name property.

3.2. Ignore with Include.NON_NULL on the ObjectMapper

Secondly, we change the default settings of Jackson:

@Test
public void shouldSerialize_WithNonNullInclusion_OnMapper() throws JsonProcessingException {

    class Car {

        private String name;
        private int age = 12;

        public String getName() {
            return name;
        }

        public int getAge() {
            return age;
        }
    }

    Car car = new Car();

    objectMapper.setSerializationInclusion(Include.NON_NULL);
    String json = objectMapper.writeValueAsString(car);

    assertThat(json).isEqualTo("{\"age\":12}");
}

Here, the ObjectMapper instance is configured to ignore null values using Include.NON_NULL. As a result, all serialization operations should discard null values.

objectMapper.setSerializationInclusion(Include.NON_NULL);

 

3.3. Other Include Values

Lastly, the Include enum also contains other values which we’ll list here for reference:

public enum Include
    {
        ALWAYS,
        NON_NULL,
        NON_ABSENT,
        NON_EMPTY,
        NON_DEFAULT,
        USE_DEFAULTS;
    }

4. Summary

In this tutorial, we’ve looked at how we can ignore null values during serialization using Jackson.

As always, the source code is available on Github.

Leave a Reply

Close Menu