🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment