Java Jackson @JsonSerialize

1. Overview

Jackson offers a powerful suite of annotations to customize the way Java objects are serialized into JSON and vice versa. One of the most versatile annotations is @JsonSerialize, which is employed to specify a custom serializer for an object. In this tutorial, we'll delve into the utilization of this annotation using an Employee Management System as our example.

@JsonSerialize Annotation Overview

The @JsonSerialize annotation in Jackson is employed to define a custom serializer for a Java object or property. It grants developers increased control over the JSON output, allowing for more tailored serialization than what's provided out-of-the-box by Jackson.

2. Development Steps

1. Establish a new Maven project.

2. Add the requisite Jackson dependencies.

3. Construct an Employee class, employing the @JsonSerialize annotation.

4. Define a custom serializer class.

5. Create a demonstration class with the main method.

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.13.0</version>
</dependency>

5. Code Program

For our Employee Management System, consider the subsequent classes:
// Employee.java
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
public class Employee {
    private int empId;
    private String empName;
    private double salary;
    @JsonSerialize(using = SalarySerializer.class)
    public double getSalary() {
        return salary;
    }
    public Employee(int empId, String empName, double salary) {
        this.empId = empId;
        this.empName = empName;
        this.salary = salary;
    }
    public int getEmpId() {
        return empId;
    }
    public String getEmpName() {
        return empName;
    }
}

// SalarySerializer.java
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;
public class SalarySerializer extends JsonSerializer<Double> {
    @Override
    public void serialize(Double value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        // Display salary rounded to two decimal places
        gen.writeString(String.format("%.2f", value));
    }
}

// MainClass.java
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class MainClass {
    public static void main(String[] args) {
        Employee employee = new Employee(1, "Alice", 5000.4567);
        ObjectMapper mapper = new ObjectMapper();
        try {
            String json = mapper.writeValueAsString(employee);
            System.out.println("Serialized JSON: " + json);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }
}

Output:

Serialized JSON: {"empId":1,"empName":"Alice","salary":"5000.46"}

Code Explanation:

In the Employee class, the salary property uses the @JsonSerialize annotation to specify SalarySerializer as its serializer. This custom serializer, SalarySerializer, modifies the serialization of the salary property to round to two decimal places. The MainClass shows

Comments