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