Detailed Explanation of Jackson Annotations - Notes By ShariqSP

Detailed Explanation of Jackson Annotations

Jackson provides a wide range of annotations to customize JSON serialization and deserialization. These annotations allow developers to control how Java objects are mapped to JSON and vice versa. Below is a detailed explanation of commonly used Jackson annotations, along with examples for each.

1. @JsonPropertyOrder

Used to specify the order in which properties appear in the serialized JSON. Without this annotation, the property order may not be consistent.

Example:

import com.fasterxml.jackson.annotation.JsonPropertyOrder;
            
            @JsonPropertyOrder({"id", "username", "email", "mobile"})
            class User {
                private int id;
                private String username;
                private String email;
                private String mobile;
            
                // Getters and setters
            }

Output:

{
              "id": 1,
              "username": "shariq",
              "email": "shariq@shariqsp.com",
              "mobile": "9876543210"
            }

This ensures a predictable and logical property order in the output JSON.

2. @JsonProperty

This annotation is used to specify the JSON property name for a Java field. By default, Jackson uses the field name as the property name.

Example:

import com.fasterxml.jackson.annotation.JsonProperty;
            
            class User {
                @JsonProperty("user_id")
                private int id;
            
                @JsonProperty("user_name")
                private String username;
            
                private String email;
            
                // Getters and setters
            }

Output:

{
              "user_id": 1,
              "user_name": "shariq",
              "email": "shariq@shariqsp.com"
            }

3. @JsonIgnore

Prevents a field from being serialized or deserialized.

Example:

import com.fasterxml.jackson.annotation.JsonIgnore;
            
            class User {
                private int id;
            
                private String username;
            
                @JsonIgnore
                private String password;
            
                // Getters and setters
            }

Output:

{
              "id": 1,
              "username": "shariq"
            }

4. @JsonInclude

Controls whether null or empty values are included in the JSON output.

Example:

import com.fasterxml.jackson.annotation.JsonInclude;
            
            @JsonInclude(JsonInclude.Include.NON_NULL)
            class User {
                private int id;
                private String username;
                private String email; // Will be excluded if null
            
                // Getters and setters
            }

Output:

If email is null:

{
              "id": 1,
              "username": "shariq"
            }

5. @JsonFormat

Used to format dates or numbers during serialization or deserialization.

Example:

import com.fasterxml.jackson.annotation.JsonFormat;
            import java.util.Date;
            
            class Event {
                private String name;
            
                @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
                private Date eventDate;
            
                // Getters and setters
            }

Output:

{
              "name": "Conference",
              "eventDate": "2024-11-22"
            }

6. @JsonCreator

Used to define how a JSON object is mapped to a class constructor or factory method during deserialization.

Example:

import com.fasterxml.jackson.annotation.JsonCreator;
            import com.fasterxml.jackson.annotation.JsonProperty;
            
            class User {
                private int id;
                private String username;
            
                @JsonCreator
                public User(@JsonProperty("id") int id, @JsonProperty("username") String username) {
                    this.id = id;
                    this.username = username;
                }
            
                // Getters
            }

Input JSON:

{
              "id": 1,
              "username": "shariq"
            }

Deserialized Object:

User{id=1, username="shariq"}

7. @JsonSetter

Specifies a custom setter for a JSON property.

Example:

import com.fasterxml.jackson.annotation.JsonSetter;
            
            class User {
                private String username;
            
                @JsonSetter("user_name")
                public void setUsername(String username) {
                    this.username = username;
                }
            
                // Getter
            }

Input JSON:

{
              "user_name": "shariq"
            }

Result: The setUsername method will be invoked with the value "shariq".

8. @JsonIgnoreProperties

Ignores specified properties globally during serialization or deserialization.

Example:

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
            
            @JsonIgnoreProperties({"email", "password"})
            class User {
                private int id;
                private String username;
                private String email;
                private String password;
            
                // Getters and setters
            }

Output:

{
              "id": 1,
              "username": "shariq"
            }

9. @JsonAnyGetter and @JsonAnySetter

Used to dynamically serialize or deserialize properties that are not predefined in the class.

Example:

import com.fasterxml.jackson.annotation.JsonAnyGetter;
            import com.fasterxml.jackson.annotation.JsonAnySetter;
            import java.util.HashMap;
            import java.util.Map;
            
            class User {
                private Map<String, Object> properties = new HashMap<>();
            
                @JsonAnySetter
                public void addProperty(String key, Object value) {
                    properties.put(key, value);
                }
            
                @JsonAnyGetter
                public Map<String, Object> getProperties() {
                    return properties;
                }
            }

Input JSON:

{
              "id": 1,
              "username": "shariq",
              "extraProperty": "value"
            }

Deserialized Object: The extraProperty will be stored in the properties map.

Key Points

  • Jackson annotations offer granular control over JSON serialization and deserialization.
  • Annotations can be combined for advanced use cases.
  • Ensure the class structure matches your JSON structure to avoid runtime errors.