Java Jackson @JsonIgnore Example

1. Introduction

While working with JSON data using the Jackson library in Java, there are scenarios where you might want to exclude certain fields from serialization (Java to JSON) or deserialization (JSON to Java). The @JsonIgnore annotation offers a handy solution for this, enabling developers to specify fields that should be ignored during these processes.

2. Example Steps

1. Create a Java class Person with several fields.

2. Apply the @JsonIgnore annotation to fields we want to exclude from serialization and deserialization.

3. Serialize and deserialize a Person object to demonstrate the effect of the annotation.

3. Java Jackson @JsonIgnore Example

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.annotation.JsonIgnore;

public class JacksonJsonIgnoreExample {

    public static void main(String[] args) throws Exception {
        // Create a new Person object
        Person person = new Person("Jane", "Doe", 30, "password123");

        // Initialize ObjectMapper
        ObjectMapper mapper = new ObjectMapper();

        // Serialize the Person object to JSON
        String jsonString = mapper.writeValueAsString(person);

        // Print the serialized JSON
        System.out.println(jsonString);

        // Deserialize JSON string to a Person object
        Person deserializedPerson = mapper.readValue("{\"firstName\":\"John\",\"lastName\":\"Doe\",\"age\":25,\"password\":\"hidden\"}", Person.class);

        // Print the deserialized Person object
        System.out.println(deserializedPerson);
    }
}

class Person {
    private String firstName;
    private String lastName;
    private int age;

    // Applying @JsonIgnore to exclude this field from serialization and deserialization
    @JsonIgnore
    private String password;

    // Constructor, getters, and setters
    public Person(String firstName, String lastName, int age, String password) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
        this.password = password;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public int getAge() {
        return age;
    }

    public String getPassword() {
        return password;
    }

    @Override
    public String toString() {
        return "Person [firstName=" + firstName + ", lastName=" + lastName + ", age=" + age + "]";
    }
}

Output:

{"firstName":"Jane","lastName":"Doe","age":30}
Person [firstName=John, lastName=Doe, age=25]

4. Step By Step Explanation

In the provided code, we created a Person class with fields like firstName, lastName, age, and password. To ensure that the password field isn't exposed during serialization or altered during deserialization, we applied the @JsonIgnore annotation to it.

When the ObjectMapper serialized the Person object to a JSON string, the password field was excluded. Similarly, during deserialization, even if the JSON contains a password property, it is ignored and doesn't impact the resulting Java object.


Comments