Mastering Jackson Annotations in Java: Real-World Examples

araf injazataraf injazat
2 min read

Jackson is a widely-used library in Java for processing JSON data. Its powerful annotations allow developers to control how Java objects are serialized into JSON and deserialized back into Java objects. In this post, we'll explore some of the most useful Jackson annotations with practical examples.

Setup

First, add Jackson to your pom.xml:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.15.0</version>
</dependency>

1. @JsonProperty: Rename Fields

Use @JsonProperty to map JSON property names to Java fields when they don't match.

public class User {
    @JsonProperty("full_name")
    private String name;
}

2. @JsonIgnore: Exclude Fields

Prevent sensitive data from being serialized.

public class User {
    private String name;

    @JsonIgnore
    private String password;
}

3. @JsonInclude: Conditional Serialization

Avoid sending nulls in JSON payload.

@JsonInclude(JsonInclude.Include.NON_NULL)
public class Product {
    private String name;
    private String description;
}

4. @JsonFormat: Format Dates or Numbers

Perfect for readable date/time serialization.

public class Event {
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm")
    private LocalDateTime dateTime;
}

5. @JsonIgnoreProperties: Ignore Unmapped JSON Fields

Avoid errors from unexpected JSON fields.

@JsonIgnoreProperties(ignoreUnknown = true)
public class Customer {
    private String id;
    private String name;
}

6. @JsonCreator + @JsonProperty: Constructor-Based Deserialization

Used when there's no default constructor.

public class Account {
    private final String id;
    private final String type;

    @JsonCreator
    public Account(@JsonProperty("id") String id, @JsonProperty("type") String type) {
        this.id = id;
        this.type = type;
    }
}

7. @JsonValue: Custom Enum Serialization

Control how enums are represented.

public enum Status {
    ACTIVE("active"),
    INACTIVE("inactive");

    private final String value;

    Status(String value) {
        this.value = value;
    }

    @JsonValue
    public String getValue() {
        return value;
    }
}

8. @JsonAlias: Support Multiple JSON Field Names

Makes deserialization more flexible.

public class UserDTO {
    @JsonAlias({"fullName", "user_name"})
    private String name;
}

9. @JsonAnySetter: Dynamic JSON Properties to Map

Great for handling dynamic key-value pairs.

public class Config {
    private Map<String, String> settings = new HashMap<>();

    @JsonAnySetter
    public void addSetting(String key, String value) {
        settings.put(key, value);
    }
}

10. @JsonAnyGetter: Dynamic Map to JSON

Serialize a Map as standard JSON properties.

public class Config {
    private Map<String, String> settings;

    @JsonAnyGetter
    public Map<String, String> getSettings() {
        return settings;
    }
}

By mastering these annotations, you can have fine-grained control over your JSON serialization and deserialization processes in Java. Happy coding!

#java #jackson #json #serialization #springboot

0
Subscribe to my newsletter

Read articles from araf injazat directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

araf injazat
araf injazat