Java Jackson @JsonSetter

1. Overview

Jackson is a popular library in Java for parsing JSON. While there are many annotations available in Jackson to control the serialization and deserialization of Java objects, the @JsonSetter annotation stands out when it comes to customizing the process of setting values from JSON properties to Java fields. In this blog post, we'll dive deep into its usage within the context of an Employee Management System.

@JsonSetter Annotation Overview

The @JsonSetter annotation is used to indicate the method to be used for setting a value from JSON. While most of the time the naming convention of JavaBeans is sufficient for Jackson to determine which setter method to use, there can be scenarios where the JSON property name does not match the Java field name. In such cases, @JsonSetter becomes instrumental.

2. Development Steps

1. Create a new Maven project.

2. Add necessary Jackson dependencies.

3. Design an Employee class using the @JsonSetter annotation.

4. Develop a main class for demonstration.

5. Deserialize a JSON string into an Employee object.

3. Create a Maven Project

There are different ways to create a simple Maven project:

Create a Simple Maven Project using the Command Line Interface

Create a Simple Maven Project using  Eclipse IDE

Create a Simple Maven Project using  IntelliJ IDEA

4. Maven Dependencies

Open the pom.xml file, and add the following Jackson data binding dependency:

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

5. Code Program

For our Employee Management System, consider the following classes:
// Employee.java
public class Employee {
    private int id;
    private String fullName;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getFullName() {
        return fullName;
    }
    @JsonSetter("name")
    public void setFullName(String fullName) {
        this.fullName = fullName;
    }
}

// MainDemo.java
import com.fasterxml.jackson.databind.ObjectMapper;
public class MainDemo {
    public static void main(String[] args) {
        String json = "{\"id\":1,\"name\":\"John Doe\"}";
        ObjectMapper mapper = new ObjectMapper();
        try {
            Employee employee = mapper.readValue(json, Employee.class);
            System.out.println("Employee ID: " + employee.getId());
            System.out.println("Employee Full Name: " + employee.getFullName());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Output:

Employee ID: 1
Employee Full Name: John Doe

Code Explanation:

Here, in the Employee class, the Java field fullName is intended to hold the value of the JSON property name

Using the @JsonSetter("name") annotation, we inform Jackson to invoke the setFullName method whenever it encounters the name property in the JSON. 

This is especially handy when JSON property names don't align with the Java field names or when adhering to certain naming conventions in Java.

6. Conclusion

Jackson's @JsonSetter annotation offers a clean and intuitive way to control the deserialization of JSON properties into Java fields. Its ability to bridge the gap between disparate naming conventions ensures that our Java applications remain robust and adaptable in the face of changing JSON structures.

Comments