🎓 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 prominent Java library known for converting Java objects to JSON and vice versa. At times, we might want to include specific fields in the JSON output only when they meet certain criteria, and the @JsonInclude annotation in Jackson is tailor-made for this purpose.
@JsonInclude Annotation Overview
The @JsonInclude annotation defines the criteria for including properties in the JSON output during serialization. Typically, it is used to prevent properties with null values or default states from being serialized, making the resultant JSON more concise and readable.
2. Development Steps
1. Initiate a new Maven project.
2. Add the necessary Jackson dependencies.
3. Craft the User class and utilize the @JsonInclude annotation.
4. Create a separate class for serialization logic.
5. Formulate a main class to showcase 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
Using the User Management System as a reference, let's delve in:
// User.java
import com.fasterxml.jackson.annotation.JsonInclude;
@JsonInclude(JsonInclude.Include.NON_NULL)
public class User {
private String username;
private String email;
private String phone;
// Suppose a user doesn't have a phone number, we don't want to serialize it.
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private String[] roles;
// 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
public class MainClass {
public static void main(String[] args) {
User user = new User();
user.setUsername("alice");
user.setEmail("alice@email.com");
// No phone and roles set for this user
try {
String json = JsonSerializer.serializeToJson(user);
System.out.println(json);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
}
Output:
{"username":"alice","email":"alice@email.com"}
Code Explanation:
The User class comprises fields like username, email, phone, and roles.
Using the @JsonInclude annotation, we specified that we only want non-null values to be serialized. As such, properties with null values, like phone and roles in this case, aren't included in the final JSON output.
The JsonSerializer class simplifies serialization, employing Jackson's ObjectMapper, and MainClass integrates the logic, outputting the JSON representation of the User instance.
6. Conclusion
The @JsonInclude annotation in Jackson provides an intuitive and effective means for developers to filter out undesired properties from their JSON outputs, thereby ensuring a cleaner, more precise representation of data structures in JSON format.
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