Jackson-এর @JsonIgnore এবং @JsonInclude হলো দুটি গুরুত্বপূর্ণ অ্যানোটেশন যা JSON ডেটা সিরিয়ালাইজেশন এবং ডেসিরিয়ালাইজেশন প্রক্রিয়াকে নিয়ন্ত্রণ করতে ব্যবহার করা হয়।
১. @JsonIgnore
@JsonIgnore ব্যবহার করা হয় একটি ফিল্ড বা মেথডকে JSON সিরিয়ালাইজেশন এবং ডেসিরিয়ালাইজেশন থেকে বাদ দেওয়ার জন্য।
মূল বৈশিষ্ট্য:
- JSON ডেটায় সেই ফিল্ডটি প্রদর্শিত হবে না।
- ডেসিরিয়ালাইজেশনের সময় JSON-এ থাকা সেই ফিল্ডটি মডেলে সেট হবে না।
ব্যবহার:
import com.fasterxml.jackson.annotation.JsonIgnore;
public class User {
private String name;
@JsonIgnore
private String password; // JSON ডেটা থেকে বাদ যাবে
public User(String name, String password) {
this.name = name;
this.password = password;
}
// Getters এবং Setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
উদাহরণ:
Serialization (Java Object → JSON):
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonIgnoreExample {
public static void main(String[] args) throws Exception {
User user = new User("John Doe", "secret");
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(user);
System.out.println(json); // Output: {"name":"John Doe"}
}
}
Deserialization (JSON → Java Object):
public class JsonIgnoreExample {
public static void main(String[] args) throws Exception {
String json = "{\"name\":\"John Doe\",\"password\":\"secret\"}";
ObjectMapper objectMapper = new ObjectMapper();
User user = objectMapper.readValue(json, User.class);
System.out.println(user.getPassword()); // Output: null
}
}
২. @JsonInclude
@JsonInclude ব্যবহার করা হয় JSON-এ কেবল নির্দিষ্ট মানের শর্ত পূরণ হলে ফিল্ড অন্তর্ভুক্ত করতে। এটি JSON ডেটা সিরিয়ালাইজেশনের সময় প্রযোজ্য।
মূল বৈশিষ্ট্য:
- ডিফল্ট বা নির্ধারিত মান ব্যতীত অন্যান্য মান JSON-এ অন্তর্ভুক্ত হয়।
- অপ্রয়োজনীয় বা
nullডেটা JSON থেকে সরিয়ে দেয়।
ব্যবহার:
import com.fasterxml.jackson.annotation.JsonInclude;
@JsonInclude(JsonInclude.Include.NON_NULL) // শুধুমাত্র null নয় এমন ফিল্ড অন্তর্ভুক্ত হবে
public class User {
private String name;
private String email; // null হলে JSON-এ অন্তর্ভুক্ত হবে না
public User(String name, String email) {
this.name = name;
this.email = email;
}
// Getters এবং Setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
Include Options:
Jackson @JsonInclude-এর বিভিন্ন অপশন প্রদান করে:
Include.NON_NULL: কেবলমাত্র null নয় এমন ফিল্ড অন্তর্ভুক্ত হয়।Include.NON_EMPTY: null বা খালি ("",[],{}) নয় এমন ফিল্ড অন্তর্ভুক্ত হয়।Include.NON_DEFAULT: ডিফল্ট মান নয় এমন ফিল্ড অন্তর্ভুক্ত হয়।Include.ALWAYS: সব ফিল্ড অন্তর্ভুক্ত হয় (ডিফল্ট)।
উদাহরণ:
Serialization:
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonIncludeExample {
public static void main(String[] args) throws Exception {
User user = new User("John Doe", null); // email null
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(user);
System.out.println(json); // Output: {"name":"John Doe"}
}
}
উপযোগিতা এবং পার্থক্য
| বৈশিষ্ট্য | @JsonIgnore | @JsonInclude |
|---|---|---|
| মূল কাজ | JSON-এ ফিল্ড সম্পূর্ণভাবে বাদ দেয়। | নির্দিষ্ট শর্ত পূরণ না হলে JSON-এ ফিল্ড অন্তর্ভুক্ত হয় না। |
| প্রভাবিত ফিল্ড | সবসময় JSON থেকে বাদ দেয়। | null, খালি, বা ডিফল্ট মানের ক্ষেত্রে কাজ করে। |
| সিরিয়ালাইজেশন | JSON ডেটা তৈরির সময় কাজ করে। | JSON ডেটা তৈরির সময় শর্ত সাপেক্ষে কাজ করে। |
| ডেসিরিয়ালাইজেশন | JSON ডেটা থেকে ফিল্ড সেট হতে দেয় না। | ডেসিরিয়ালাইজেশনে প্রভাব ফেলে না। |
সমন্বিত ব্যবহার উদাহরণ
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
@JsonInclude(JsonInclude.Include.NON_NULL)
public class User {
private String name;
@JsonIgnore
private String password; // JSON-এ প্রদর্শিত হবে না
private String email; // null হলে JSON-এ অন্তর্ভুক্ত হবে না
public User(String name, String password, String email) {
this.name = name;
this.password = password;
this.email = email;
}
// Getters এবং Setters
}
Serialization Output:
User user = new User("John Doe", "secret", null);
String json = objectMapper.writeValueAsString(user);
System.out.println(json);
Output:
{
"name": "John Doe"
}
@JsonIgnore: JSON থেকে একটি নির্দিষ্ট ফিল্ড সম্পূর্ণ বাদ দিতে ব্যবহার হয়।@JsonInclude: JSON-এ শর্ত সাপেক্ষে ফিল্ড অন্তর্ভুক্ত করতে ব্যবহার হয়।- ব্যবহার ক্ষেত্র: RESTful API, ডেটা কাস্টমাইজেশন, এবং নিরাপত্তা নিশ্চিত করতে এই অ্যানোটেশনগুলো খুবই উপযোগী।
Jackson এর @JsonIgnore অ্যানোটেশন ব্যবহার করে নির্দিষ্ট ফিল্ড JSON Serialization এবং Deserialization এর সময় উপেক্ষা করা যায়। এটি এমন ক্ষেত্রগুলোর জন্য উপযোগী, যেগুলো JSON আউটপুটে থাকা উচিত নয় অথবা ইনপুট হিসেবে ব্যবহার করা উচিত নয়।
@JsonIgnore এর ব্যবহার
১. মডেল ক্লাস তৈরি
import com.fasterxml.jackson.annotation.JsonIgnore;
public class User {
private String name;
private int age;
@JsonIgnore // এই ফিল্ডটি JSON থেকে বাদ যাবে
private String password;
// Getters and Setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
২. Serialization (Java Object থেকে JSON)
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonIgnoreExample {
public static void main(String[] args) throws Exception {
// User Object তৈরি
User user = new User();
user.setName("Alice");
user.setAge(30);
user.setPassword("secret123");
// ObjectMapper ব্যবহার করে JSON তৈরি
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(user);
System.out.println("Serialized JSON: " + json);
}
}
Output:
{"name":"Alice","age":30}
ব্যাখ্যা:
passwordফিল্ডে@JsonIgnoreঅ্যাপ্লাই করা হয়েছে, তাই এটি JSON আউটপুটে অন্তর্ভুক্ত হয়নি।
৩. Deserialization (JSON থেকে Java Object)
public class JsonIgnoreDeserializationExample {
public static void main(String[] args) throws Exception {
String json = "{\"name\":\"Bob\",\"age\":25,\"password\":\"shouldBeIgnored\"}";
ObjectMapper mapper = new ObjectMapper();
User user = mapper.readValue(json, User.class);
System.out.println("Name: " + user.getName());
System.out.println("Age: " + user.getAge());
System.out.println("Password: " + user.getPassword()); // null থাকবে
}
}
Output:
Name: Bob
Age: 25
Password: null
ব্যাখ্যা:
- JSON-এ
passwordফিল্ড উপস্থিত থাকলেও এটি@JsonIgnoreএর কারণেUserঅবজেক্টে সেট হয়নি।
@JsonIgnore এবং @JsonProperty একত্রে ব্যবহার
কখনও কখনও আপনি JSON থেকে ফিল্ড বাদ দিতে চান, কিন্তু Deserialization এর সময় এটি সেট করতে চান। এ জন্য @JsonProperty ব্যবহার করা যেতে পারে।
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
public class User {
private String name;
@JsonIgnore
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY) // শুধুমাত্র Deserialization এর জন্য অনুমোদিত
private String password;
// Getters and Setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
Serialization Output:
{
"name": "Alice"
}
Deserialization Input:
{
"name": "Bob",
"password": "securePassword"
}
Deserialized Object:
Name: Bob
Password: securePassword
@JsonIgnore এর সীমাবদ্ধতা
@JsonIgnoreফিল্ডে শুধুSerializationএবংDeserializationএর সময় কাজ করে।- যদি কোনো ফিল্ড সম্পূর্ণভাবে Exclude করার প্রয়োজন হয়, তবে
@JsonIgnorePropertiesব্যবহার করতে পারেন।
@JsonIgnoreProperties ব্যবহার
মাল্টিপল ফিল্ড Exclude করার জন্য:
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties({"password", "email"}) // একাধিক ফিল্ড বাদ দেওয়া
public class User {
private String name;
private int age;
private String password;
private String email;
// Getters and Setters
}
@JsonIgnoreব্যবহার করে নির্দিষ্ট ফিল্ড JSON আউটপুট বা ইনপুট থেকে বাদ দেওয়া যায়।- Serialization: JSON এ শুধুমাত্র প্রয়োজনীয় ডেটা প্রকাশ করা যায়।
- Deserialization: অপ্রয়োজনীয় বা সংবেদনশীল ডেটা সেট হওয়া এড়ানো যায়।
- আরও ফাইন-গ্রেইন কন্ট্রোল প্রয়োজন হলে
@JsonPropertyএবং@JsonIgnorePropertiesব্যবহার করা যেতে পারে।
Jackson-এ @JsonIgnoreProperties অ্যানোটেশন ব্যবহার করে serialization এবং deserialization এর সময় এক বা একাধিক ফিল্ড JSON থেকে বাদ দেওয়া যায়। এটি তখন কার্যকর হয় যখন আপনি মডেল ক্লাসের কিছু ফিল্ড JSON-এ অন্তর্ভুক্ত করতে না চান।
1. @JsonIgnoreProperties এর ব্যবহার
@JsonIgnoreProperties পুরো ক্লাসের লেভেলে নির্দিষ্ট ফিল্ডগুলিকে JSON থেকে বাদ দিতে ব্যবহৃত হয়।
Example:
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties({"password", "ssn"})
public class User {
public int id;
public String name;
public String email;
public String password; // Excluded
public String ssn; // Excluded
}
Serialization কোড:
import com.fasterxml.jackson.databind.ObjectMapper;
public class Main {
public static void main(String[] args) throws Exception {
User user = new User();
user.id = 101;
user.name = "John Doe";
user.email = "john.doe@example.com";
user.password = "secret";
user.ssn = "123-45-6789";
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(user);
System.out.println("Serialized JSON: " + json);
}
}
আউটপুট:
{
"id": 101,
"name": "John Doe",
"email": "john.doe@example.com"
}
2. Dynamic Field Exclusion
যদি runtime-এ নির্দিষ্ট ফিল্ড বাদ দিতে চান, তাহলে ObjectMapper কনফিগার করতে পারেন।
Dynamic Example:
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
public class Main {
public static void main(String[] args) throws Exception {
User user = new User();
user.id = 101;
user.name = "John Doe";
user.email = "john.doe@example.com";
user.password = "secret";
user.ssn = "123-45-6789";
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
// Exclude specific fields dynamically
objectMapper.addMixIn(User.class, IgnoreFieldsMixin.class);
String json = objectMapper.writeValueAsString(user);
System.out.println("Dynamic Exclusion JSON: " + json);
}
}
@JsonIgnoreProperties({"password", "ssn"})
abstract class IgnoreFieldsMixin {}
আউটপুট:
{
"id": 101,
"name": "John Doe",
"email": "john.doe@example.com"
}
3. @JsonIgnoreProperties এর সাথে Deserialization
Deserialization এর সময় unknown properties এড়ানোর জন্য @JsonIgnoreProperties(ignoreUnknown = true) ব্যবহার করা হয়।
Example:
@JsonIgnoreProperties(ignoreUnknown = true)
public class User {
public int id;
public String name;
public String email;
}
কোড:
import com.fasterxml.jackson.databind.ObjectMapper;
public class Main {
public static void main(String[] args) throws Exception {
String json = """
{
"id": 101,
"name": "John Doe",
"email": "john.doe@example.com",
"extraField": "should be ignored"
}
""";
ObjectMapper objectMapper = new ObjectMapper();
User user = objectMapper.readValue(json, User.class);
System.out.println("Deserialized User: " + user.name);
}
}
আউটপুট:
Deserialized User: John Doe
4. Multiple Field Exclusion Example
Class Definition:
@JsonIgnoreProperties({"password", "ssn", "creditCardNumber"})
public class User {
public int id;
public String name;
public String email;
public String password;
public String ssn;
public String creditCardNumber;
}
Serialization Example:
import com.fasterxml.jackson.databind.ObjectMapper;
public class Main {
public static void main(String[] args) throws Exception {
User user = new User();
user.id = 101;
user.name = "John Doe";
user.email = "john.doe@example.com";
user.password = "secret";
user.ssn = "123-45-6789";
user.creditCardNumber = "4111-1111-1111-1111";
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(user);
System.out.println("Serialized JSON: " + json);
}
}
আউটপুট:
{
"id": 101,
"name": "John Doe",
"email": "john.doe@example.com"
}
5. Field Exclusion with @JsonIgnore vs @JsonIgnoreProperties
| Feature | @JsonIgnore | @JsonIgnoreProperties |
|---|---|---|
| Scope | Individual fields | Multiple fields (Class level) |
| Serialization Exclusion | Yes | Yes |
| Deserialization Exclusion | Yes | Yes |
| ignoreUnknown Support | No | Yes |
- @JsonIgnoreProperties ক্লাস লেভেলে একাধিক ফিল্ড JSON থেকে বাদ দিতে কার্যকর।
- Dynamic Exclusion এর জন্য
ObjectMapper-এর মিক্সিন ব্যবহার করা যেতে পারে। - Deserialization-এ unknown properties এড়াতে ignoreUnknown ফ্ল্যাগ ব্যবহার করুন।
- যদি ফিল্ড লেভেলে নির্দিষ্ট ফিল্ড বাদ দিতে চান, তাহলে @JsonIgnore ব্যবহার করুন।
এই পদ্ধতিগুলো ব্যবহার করে Jackson এর মাধ্যমে serialization এবং deserialization আরও কাস্টমাইজ করা সম্ভব।
@JsonInclude অ্যানোটেশনটি Jackson-এ JSON serialization-এর সময় নির্দিষ্ট শর্ত অনুযায়ী ফিল্ডগুলোকে অন্তর্ভুক্ত বা বাদ দিতে ব্যবহৃত হয়। এটি null, empty, বা default values-এর মতো অপ্রয়োজনীয় ডেটা JSON আউটপুট থেকে বাদ দিতে সাহায্য করে।
1. @JsonInclude Overview
@JsonInclude কাস্টমাইজেশন করার জন্য অনেক অপশন প্রদান করে। এই অ্যানোটেশনটি ক্লাস বা ফিল্ডের উপর প্রয়োগ করা যেতে পারে।
Syntax:
@JsonInclude(JsonInclude.Include.VALUE)
Common Include Values:
Include.ALWAYS(Default): সব ফিল্ড অন্তর্ভুক্ত করে।Include.NON_NULL: শুধুমাত্র non-null ফিল্ড অন্তর্ভুক্ত করে।Include.NON_EMPTY: null এবং empty (যেমন: empty string, collection) বাদ দেয়।Include.NON_DEFAULT: ফিল্ডের default value বাদ দেয়।Include.CUSTOM: কাস্টম লজিকের মাধ্যমে serialization নিয়ন্ত্রণ করে।
2. Maven Dependency
Ensure your project has the Jackson dependency in the pom.xml:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.15.2</version>
</dependency>
3. Practical Examples of @JsonInclude
Example 1: Exclude Null Values
import com.fasterxml.jackson.annotation.JsonInclude;
@JsonInclude(JsonInclude.Include.NON_NULL)
public class User {
private String name;
private String email;
// Getters and Setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
Test Serialization:
import com.fasterxml.jackson.databind.ObjectMapper;
public class Main {
public static void main(String[] args) throws Exception {
User user = new User();
user.setName("Alice");
// Email is null
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(user);
System.out.println(json);
}
}
Output:
{
"name": "Alice"
}
Example 2: Exclude Null and Empty Values
import com.fasterxml.jackson.annotation.JsonInclude;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class User {
private String name;
private String email;
private String phoneNumber;
// Getters and Setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
}
Test Serialization:
import com.fasterxml.jackson.databind.ObjectMapper;
public class Main {
public static void main(String[] args) throws Exception {
User user = new User();
user.setName("Alice");
user.setEmail(""); // Empty String
// Phone Number is null
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(user);
System.out.println(json);
}
}
Output:
{
"name": "Alice"
}
Example 3: Exclude Default Values
import com.fasterxml.jackson.annotation.JsonInclude;
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
public class Product {
private String name;
private double price = 0.0; // Default value
// Getters and Setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
Test Serialization:
import com.fasterxml.jackson.databind.ObjectMapper;
public class Main {
public static void main(String[] args) throws Exception {
Product product = new Product();
product.setName("Laptop");
// Price is default (0.0)
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(product);
System.out.println(json);
}
}
Output:
{
"name": "Laptop"
}
4. Applying @JsonInclude at Class and Field Level
Field-Level Application:
public class User {
@JsonInclude(JsonInclude.Include.NON_NULL)
private String name;
private String email;
// Getters and Setters
}
Class-Level Application:
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class User {
private String name;
private String email;
// Getters and Setters
}
5. Combine @JsonInclude with @JsonProperty
You can combine @JsonInclude with @JsonProperty for fine-grained control.
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
@JsonInclude(JsonInclude.Include.NON_NULL)
public class User {
@JsonProperty("full_name")
private String name;
@JsonProperty("user_email")
private String email;
// Getters and Setters
}
Output:
{
"full_name": "Alice"
}
6. Exclude Null and Empty Globally
Instead of using @JsonInclude on each class, configure it globally using ObjectMapper.
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Main {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
User user = new User();
user.setName("Alice");
user.setEmail(""); // Empty String
String json = mapper.writeValueAsString(user);
System.out.println(json);
}
}
Output:
{
"name": "Alice"
}
7. Key Points
- NON_NULL: Null values বাদ দেয়।
- NON_EMPTY: Null এবং empty string/collection বাদ দেয়।
- NON_DEFAULT: Default values বাদ দেয়।
- Globally ObjectMapper-এ configuration করে প্রজেক্ট জুড়ে নিয়ন্ত্রণ করা যায়।
@JsonIncludeJSON serialization নিয়ন্ত্রণের জন্য একটি শক্তিশালী টুল।- Null এবং empty values বাদ দিয়ে JSON ডেটা আরও পরিষ্কার ও সংক্ষিপ্ত করা যায়।
- Field বা Class লেভেলে ব্যবহার করে কাস্টমাইজেশনের সুবিধা পাওয়া যায়।
- Globally configuration করে পুরো প্রজেক্টে একই নিয়ম প্রয়োগ করা সম্ভব।
Jackson-এ @JsonIgnore এবং @JsonInclude দুটি গুরুত্বপূর্ণ অ্যানোটেশন যা JSON সিরিয়ালাইজ এবং ডি-সিরিয়ালাইজেশনের সময় ফিল্ডগুলোর অন্তর্ভুক্তি বা বাদ দেয়ার জন্য ব্যবহৃত হয়। এগুলো ব্যবহার করে JSON ডেটা কাস্টমাইজ করা সম্ভব। নিচে প্র্যাকটিকাল উদাহরণ সহ এই দুটি অ্যানোটেশনের ব্যবহার দেখানো হলো:
1. @JsonIgnore
@JsonIgnore ব্যবহার করে একটি নির্দিষ্ট ফিল্ড JSON সিরিয়ালাইজ বা ডি-সিরিয়ালাইজ থেকে বাদ দেয়া যায়। এটি তখন প্রয়োজন হয় যখন কোনো ডেটা নিরাপত্তাজনিত কারণে JSON-এ অন্তর্ভুক্ত করা যাবে না।
ব্যবহার:
- সিরিয়ালাইজেশনের সময়: JSON-এ ফিল্ড অন্তর্ভুক্ত হবে না।
- ডি-সিরিয়ালাইজেশনের সময়: JSON থেকে ফিল্ড পড়া হবে না।
উদাহরণ:
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonIgnoreExample {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
// Create an object
User user = new User("John Doe", "password123", "john.doe@example.com");
// Serialize the object to JSON
String json = mapper.writeValueAsString(user);
System.out.println("Serialized JSON: " + json);
// Deserialize JSON to object
String inputJson = "{\"name\":\"John Doe\",\"password\":\"password123\",\"email\":\"john.doe@example.com\"}";
User deserializedUser = mapper.readValue(inputJson, User.class);
System.out.println("Deserialized User: " + deserializedUser);
}
}
class User {
private String name;
@JsonIgnore
private String password;
private String email;
public User() {}
public User(String name, String password, String email) {
this.name = name;
this.password = password;
this.email = email;
}
// Getters and Setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "User{name='" + name + "', email='" + email + "'}";
}
}
Output:
Serialized JSON: {"name":"John Doe","email":"john.doe@example.com"}
Deserialized User: User{name='John Doe', email='john.doe@example.com'}
- Password ফিল্ড JSON-এ অন্তর্ভুক্ত হয়নি।
2. @JsonInclude
@JsonInclude ব্যবহার করে কাস্টম কন্ডিশনের উপর ভিত্তি করে নির্দিষ্ট ফিল্ডগুলো JSON-এ অন্তর্ভুক্ত করা হয়। এটি তখন ব্যবহার করা হয় যখন কোনো ফিল্ডের ভ্যালু null, default, বা নির্দিষ্ট কন্ডিশনের আওতায় থাকে।
ব্যবহার:
- শুধুমাত্র প্রয়োজনীয় ফিল্ডগুলো JSON-এ অন্তর্ভুক্ত করা হয়।
- মেমোরি এবং স্টোরেজ সাশ্রয়ের জন্য কার্যকর।
উদাহরণ:
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonIncludeExample {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
// Create an object
Product product = new Product("Laptop", null, 0);
// Serialize the object to JSON
String json = mapper.writeValueAsString(product);
System.out.println("Serialized JSON: " + json);
}
}
@JsonInclude(JsonInclude.Include.NON_NULL) // Exclude null fields
class Product {
private String name;
private String description;
@JsonInclude(JsonInclude.Include.NON_DEFAULT) // Exclude default value (0 for int)
private int stock;
public Product() {}
public Product(String name, String description, int stock) {
this.name = name;
this.description = description;
this.stock = stock;
}
// Getters and Setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getStock() {
return stock;
}
public void setStock(int stock) {
this.stock = stock;
}
}
Output:
Serialized JSON: {"name":"Laptop"}
- Null description ফিল্ড এবং stock এর ডিফল্ট ভ্যালু (0) JSON-এ অন্তর্ভুক্ত হয়নি।
@JsonInclude এর বিভিন্ন কন্ডিশন
- Include.NON_NULL: শুধুমাত্র
nullনয় এমন ফিল্ড অন্তর্ভুক্ত হবে। - Include.NON_DEFAULT: শুধুমাত্র ডিফল্ট ভ্যালু নয় এমন ফিল্ড অন্তর্ভুক্ত হবে।
- Include.NON_EMPTY: শুধুমাত্র খালি নয় এমন ফিল্ড অন্তর্ভুক্ত হবে।
- Include.ALWAYS: সব ফিল্ড অন্তর্ভুক্ত হবে (ডিফল্ট আচরণ)।
উদাহরণ: NON_EMPTY কন্ডিশন
@JsonInclude(JsonInclude.Include.NON_EMPTY)
class Product {
private String name;
private String description;
public Product() {}
public Product(String name, String description) {
this.name = name;
this.description = description;
}
}
Output (When description is empty):
{"name":"Laptop"}
- @JsonIgnore: নিরাপত্তাজনিত কারণে বা অপ্রয়োজনীয় ডেটা বাদ দিতে ব্যবহৃত হয়।
- @JsonInclude: JSON-এ শুধুমাত্র প্রয়োজনীয় এবং কাস্টম কন্ডিশনের ডেটা অন্তর্ভুক্ত করতে ব্যবহৃত হয়।
এগুলো ব্যবহার করে JSON ডেটা মডেল আরও নির্ভুল, নিরাপদ এবং অপ্টিমাইজ করা সম্ভব।
Read more