Java Jackson @JsonRootName

1. Overview

Jackson is a renowned Java library for converting Java objects to JSON and vice versa. When we need to wrap our serialized object with a root name, Jackson's @JsonRootName annotation comes to the rescue.

@JsonRootName Annotation Overview

The @JsonRootName annotation in Jackson is employed to specify a name that will be used as the root wrapping element around the serialized object. This root name is added to the serialized JSON when the DeserializationFeature.UNWRAP_ROOT_VALUE and SerializationFeature.WRAP_ROOT_VALUE features are enabled.

2. Development Steps

1. Initiate a new Maven project.

2. Add the necessary Jackson dependencies.

3. Craft the Student class using the @JsonRootName annotation.

4. Develop a separate class for serialization.

5. Implement a main class to display serialization.

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

Diving into the Student Management System:
// Student.java
import com.fasterxml.jackson.annotation.JsonRootName;
@JsonRootName(value = "student")
public class Student {
    private String name;
    private int age;
    // Constructors, getters, setters...
}
// StudentSerializer.java
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
public class StudentSerializer {
    public static String serialize(Student student) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.enable(SerializationFeature.WRAP_ROOT_VALUE);
        return mapper.writeValueAsString(student);
    }
}

// MainClass.java
public class MainClass {
    public static void main(String[] args) {
        Student student = new Student();
        student.setName("John Doe");
        student.setAge(20);
        try {
            String json = StudentSerializer.serialize(student);
            System.out.println("Serialized JSON: " + json);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }
}

Output:

Serialized JSON: {"student":{"name":"John Doe","age":20}}

Code Explanation:

The Student class is adorned with the @JsonRootName annotation, defining the name "student" as the root wrapping element. 

The StudentSerializer class employs the ObjectMapper to serialize the student object. 

Here, we've enabled the SerializationFeature.WRAP_ROOT_VALUE feature of ObjectMapper to ensure the output JSON is wrapped with the root name specified. 

The MainClass demonstrates serialization of a Student object, resulting in JSON wrapped with the "student" root name.

6. Conclusion

Using Jackson's @JsonRootName annotation, developers can effectively wrap their serialized JSON output with a specified root name. This not only makes the JSON structure clear but also offers better organization, especially in systems that rely on structured and well-defined JSON formats.

Comments