🎓 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, a notable Java library, specializes in converting Java objects into JSON and vice versa. Sometimes, there's a need to customize the format of certain fields, especially date and time fields, during this conversion. Jackson's @JsonFormat annotation comes to the rescue in these scenarios.
@JsonFormat Annotation Overview
The @JsonFormat annotation is instrumental in specifying the format in which date, time, and number fields should be serialized and deserialized. This annotation provides fine-grained control over these formats, ensuring consistency in the representation of such fields in JSON outputs.
2. Development Steps
1. Launch a new Maven project.
2. Incorporate the essential Jackson dependencies.
3. Design the User class and apply the @JsonFormat annotation for date formatting.
4. Develop a separate class for the serialization mechanism.
5. Construct a main class to exhibit the 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
Pivoting on the User Management System as our example, let's proceed:
// User.java
import com.fasterxml.jackson.annotation.JsonFormat;
import java.util.Date;
public class User {
private String username;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy")
private Date birthdate;
// Standard constructors, getters, and setters...
}
// JsonSerializer.java
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonSerializer {
public static String serializeToJson(Object object) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
return mapper.writeValueAsString(object);
}
}
// MainClass.java
import java.util.Calendar;
public class MainClass {
public static void main(String[] args) {
User user = new User();
user.setUsername("bob");
Calendar calendar = Calendar.getInstance();
calendar.set(1990, Calendar.JANUARY, 1);
user.setBirthdate(calendar.getTime());
try {
String json = JsonSerializer.serializeToJson(user);
System.out.println(json);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
}
Output:
{"username":"bob","birthdate":"01-01-1990"}
Code Explanation:
In the User class, the birthdate field, which is of type Date, is annotated with @JsonFormat to dictate its desired format during serialization. This ensures that the date is represented in the "dd-MM-yyyy" format in the resultant JSON.
The JsonSerializer class facilitates serialization using Jackson's ObjectMapper.
MainClass serves as the driver class, creating an instance of the User, setting its attributes, and invoking the serialization logic to produce the formatted JSON output.
6. Conclusion
The @JsonFormat annotation in Jackson affords developers the versatility to precisely format date, time, and number fields during the JSON conversion process. It ensures uniformity and clarity in JSON outputs, even when dealing with complex data types.
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