Excluding Known Fields During Serialization and Deserialization

In the previous articles, we explored techniques for handling unknown fields during serialization. In this article, we’ll shift our focus to exclude known fields during serialization and deserialization by using Jackson annotations.

We’ll continue to use the Person class for our examples:

public class Person {
    private Long id;
    private String name;
    private String dateOfBirth;
    // Getters and setters (not shown for brevity)
}

Excluding Fields with @JsonIgnore

The @JsonIgnore annotation allows us to exclude specific fields from serialization. Simply apply it to the fields we want to exclude:

public class Person {
    private Long id;
    private String name;
    @JsonIgnore
    private String dateOfBirth;
    // Getters and setters (not shown for brevity)
}

With @JsonIgnore applied to the “dateOfBirth” field, it will be excluded when serializing a Person object.

    private static void ignoreJavaFields() throws JsonProcessingException {
        Person alice = new Person();
        alice.setId(1L);
        alice.setName("Alice");
        alice.setDateOfBirth("1990-05-15");

        ObjectMapper objectMapper = new ObjectMapper();
        String jsonString = objectMapper.writeValueAsString(alice);
        System.out.println("Person object to JSON String: " + jsonString);
    }

Tht output:

Person object to JSON String: {"id":1,"name":"Alice"}

Excluding Fields with @JsonIgnoreProperties

@JsonIgnoreProperties is a class level annotation, it can be used to exclude a group of fields. We can specify the names of the fields we want to exclude:

    @JsonIgnoreProperties({"id", "dateOfBirth"})
    private class Person {
        private Long id;
        private String name;
        private String dateOfBirth;
    }

In this example, the id and dateOfBirth field will be excluded during serialization because it’s listed in the @JsonIgnoreProperties annotation.

 private static void ignoreJavaFields() throws JsonProcessingException {
        Person alice = new Person();
        alice.setId(1L);
        alice.setName("Alice");
        alice.setDateOfBirth("1990-05-15");

        ObjectMapper objectMapper = new ObjectMapper();
        String jsonString = objectMapper.writeValueAsString(alice);
        System.out.println("Person object to JSON String: " + jsonString);
    }

By running the above codes, the output is:

Person object to JSON String: {"name":"Alice"}

How about Deserialization

@JsonIgnoreProperties and @JsonIgnore also work on deserialization. The value of annotated fields will be set as null even though the values has been provided in the JSON string.

    private static void ignoreJsonFields() throws JsonProcessingException {
        String json = "{\"id\":1,\"name\":\"Alice\",\"dateOfBirth\":\"1990-05-15\"}";
        ObjectMapper objectMapper = new ObjectMapper();
        Person person = objectMapper.readValue(json, Person.class);

        System.out.println("JSON String to Person object: " + person);
    }

Let’s keep @JsonIgnoreProperties({"id", "dateOfBirth"}) on Person class, after running the above codes, we got the below output. We can see that the id and dateOfBirth fields were set as null.

JSON String to Person object: Person(id=null, name=Alice, dateOfBirth=null)

Read More