1. Overview

In this tutorial, we’ll investigate how we can ignore some properties of a class during serialization and deserialization using Jackson.

2. Ignore property with @JsonIgnore on the selected property

Let’s start with the @JsonIgnore annotation.

First of all, we can put @JsonIgnore to a field, a getter method or a constructor.

2.1. @JsonIgnore during Deserialization

During deserialization, @JsonIgnore prevents mapping of the JSON field to the corresponding object field:

public class PersonWithIgnoreOnProperty {

    @JsonIgnore
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

In our sample class, the name field has the @JsonIgnore annotation.

When we try to deserialize, the name field will be null in the resulting Java object. Because we told Jackson to ignore this field.

@Test
public void shouldNotDeserialize_IgnoredOnProperty() throws IOException {
    final String json = "{\"name\":\"John\",\"age\":12}";

    PersonWithIgnoreOnProperty deserialized = objectMapper.readValue(json, PersonWithIgnoreOnProperty.class);

    assertThat(deserialized.getName()).isNull();
    assertThat(deserialized.getAge()).isEqualTo(12);
}

2.2. @JsonIgnore during Serialization

Similarly, for serialization, @JsonIgnore removes the property from the resulting JSON:

@Test
public void shouldSerialize_WithIgnoreOnField() throws JsonProcessingException {

    class Person {

        @JsonIgnore
        private int age = 12;

        public int getAge() {
            return age;
        }

        public int getAdditional() {
            return age;
        }
    }

    Person person = new Person();

    String json = objectMapper.writeValueAsString(person);

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

Here, we’re putting @JsonIgnore to the age property. As a result, Jackson ignores it during serialization and doesn’t write to the final JSON string.

2.3. Where to Add @JsonIgnore

We’ve added @JsonIgnore to a field previously, but we can also add it to methods:

@Test
public void shouldSerialize_WithIgnoreOnProperty() throws JsonProcessingException {

    class Car {

        private int age = 12;

        @JsonIgnore
        public int getAge() {
            return age;
        }

        public int getAdditional() {
            return age;
        }
    }

    Car car = new Car();

    String json = objectMapper.writeValueAsString(car);

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

Here, we’ve put the @JsonIgnore annotation to the getAge() method.

Moreover, the annotated method may not have a backing field:

@Test
public void shouldSerialize_WithIgnoreOnAdditionalProperty() throws JsonProcessingException {

    class Person {

        private int age = 12;

        public int getAge() {
            return age;
        }

        @JsonIgnore
        public int getAdditional() {
            return age;
        }
    }

    Person person = new Person();

    String json = objectMapper.writeValueAsString(person);

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

Note that getAdditional() method doesn’t have a backing field – additional.

3. Ignore Property with @JsonIgnoreProperties on the class

Now let’s look at @JsonIgnoreProperties.

The @JsonIgnoreProperties annotation enables us to set ignored properties on the class level. Moreover, it has similar semantics with @JsonIgnore:

@Test
public void shouldSerialize_WithIgnoreOnClass() throws JsonProcessingException {

    @JsonIgnoreProperties("name")
    class Person {

        private int age = 12;
        private String name = "john";

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

    Person person = new Person();

    String json = objectMapper.writeValueAsString(person);

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

Here, Jackson won’t serialize the name property.

4. Summary

In this tutorial, we’ve investigated how to ignore a property using Jackson.

As always, the source code is available on Github.

Leave a Reply

Close Menu