Explain how to use Jackson annotations like @JsonProperty, @JsonIgnore, and @JsonInclude

Introduction

  • Jackson annotations provide fine-grained control over how Java objects are serialized into JSON and deserialized back into Java objects.
  • Three commonly used annotations are:
    1. @JsonProperty: Specifies the property name to use in JSON.
    2. @JsonIgnore: Excludes a field from JSON serialization/deserialization.
    3. @JsonInclude: Controls whether a field is included in the JSON output based on specific conditions (e.g., non-null or non-empty).

1. @JsonProperty

The @JsonProperty annotation is used when you want to customize the name of a field in the JSON output or control how JSON keys are mapped to fields during deserialization.

Example:

import com.fasterxml.jackson.annotation.JsonProperty;
public class User {  

@JsonProperty("user_name")

private String name;

@JsonProperty("user_age")
private int age;

// Getters and Setters
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; }


}  

Explanation:

  • The name field is serialized as "user_name" in the JSON output.
  • The age field is serialized as "user_age".

Output JSON:

{
  "user_name": "John",
  "user_age": 30
}

2. @JsonIgnore

The @JsonIgnore annotation prevents a field from being included in either the JSON serialization or deserialization. This is useful when a field should not be exposed through JSON, such as sensitive data or transient information.

Example:

import com.fasterxml.jackson.annotation.JsonIgnore;
public class User {  

private String name;

@JsonIgnore
private String password;

// Getters and Setters
public String getName() { return name; }
public void setName(String name) { this.name = name; }

public String getPassword() { return password; }
public void setPassword(String password) { this.password = password; }


}  

Explanation:

  • The password field will be ignored during serialization and deserialization.

Output JSON:

{
  "name": "John"
}

3. @JsonInclude

The @JsonInclude annotation is used to control when a field should be included in the JSON output. You can configure it to include only non-null values, non-empty values, or other specific conditions.

Example:

import com.fasterxml.jackson.annotation.JsonInclude;
public class User {  

private String name;

@JsonInclude(JsonInclude.Include.NON_NULL)
private String address;

// Getters and Setters
public String getName() { return name; }
public void setName(String name) { this.name = name; }

public String getAddress() { return address; }
public void setAddress(String address) { this.address = address; }


}  

Explanation:

  • The address field will only be included in the JSON output if it is not null. If address is null, it will be omitted.

Output JSON (if address is null):

{
  "name": "John"
}

Output JSON (if address is not null):

{
  "name": "John",
  "address": "123 Main St"
}

Conclusion

Jackson annotations like @JsonProperty, @JsonIgnore, and @JsonInclude give you precise control over how Java objects are mapped to JSON. @JsonProperty customizes the field names in JSON, @JsonIgnore excludes specific fields from serialization, and @JsonInclude allows you to include fields conditionally, such as only when they are non-null.