Jackson ডিফল্টভাবে POJO (Plain Old Java Object) এর গেটার এবং সেটার ব্যবহার করে ডেটা সিরিয়ালাইজ ও ডেসিরিয়ালাইজ করে। তবে Constructor-based Injection এবং Immutable Class এর ক্ষেত্রে এই প্রক্রিয়া সরাসরি কাজ করে না, কারণ Immutable Class-এ সাধারণত সেটার মেথড থাকে না। এজন্য Jackson-কে কনস্ট্রাক্টর ব্যবহার করে এই ধরনের ক্লাস হ্যান্ডল করতে উপযুক্ত কনফিগারেশন প্রয়োজন।
Constructor-based Injection হ্যান্ডল করা
উদাহরণ:
একটি Immutable Class যেখানে শুধুমাত্র কনস্ট্রাক্টর ব্যবহার করে ফিল্ড ভ্যালু সেট করা হয়।
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
public class User {
private final String name;
private final int age;
// Constructor-based Injection
@JsonCreator
public User(@JsonProperty("name") String name, @JsonProperty("age") int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
ব্যাখ্যা:
@JsonCreator:- Jackson-কে নির্দেশ করে যে এই কনস্ট্রাক্টরটি ডেসিরিয়ালাইজেশনের সময় ব্যবহার করতে হবে।
@JsonProperty:- JSON ডেটার কী (key) এবং কনস্ট্রাক্টরের প্যারামিটারগুলোর মধ্যে ম্যাপিং নির্ধারণ করে।
Serialization এবং Deserialization উদাহরণ
Serialization:
import com.fasterxml.jackson.databind.ObjectMapper;
public class SerializationExample {
public static void main(String[] args) throws Exception {
User user = new User("Jackson", 30);
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(user);
System.out.println("Serialized JSON: " + json);
}
}
Output:
{
"name": "Jackson",
"age": 30
}
Deserialization:
public class DeserializationExample {
public static void main(String[] args) throws Exception {
String json = "{ \"name\": \"Jackson\", \"age\": 30 }";
ObjectMapper mapper = new ObjectMapper();
User user = mapper.readValue(json, User.class);
System.out.println("Deserialized User: Name = " + user.getName() + ", Age = " + user.getAge());
}
}
Output:
Deserialized User: Name = Jackson, Age = 30
Immutable Class হ্যান্ডল করা
Immutable Class উদাহরণ:
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
public class Employee {
private final String id;
private final String name;
@JsonCreator
public Employee(@JsonProperty("id") String id, @JsonProperty("name") String name) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
}
Serialization এবং Deserialization:
public class ImmutableClassExample {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
// Serialization
Employee employee = new Employee("E123", "John Doe");
String json = mapper.writeValueAsString(employee);
System.out.println("Serialized JSON: " + json);
// Deserialization
String inputJson = "{ \"id\": \"E123\", \"name\": \"John Doe\" }";
Employee deserializedEmployee = mapper.readValue(inputJson, Employee.class);
System.out.println("Deserialized Employee: ID = " + deserializedEmployee.getId() + ", Name = " + deserializedEmployee.getName());
}
}
Output:
Serialized JSON: {"id":"E123","name":"John Doe"}
Deserialized Employee: ID = E123, Name = John Doe
Jackson-এ Record টাইপ (Java 14+) এর সমর্থন
Java 14 থেকে Records নামে একটি ফিচার যোগ হয়েছে যা Immutable Class তৈরির জন্য একটি সহজ পদ্ধতি। Jackson স্বয়ংক্রিয়ভাবে Records হ্যান্ডল করতে পারে।
উদাহরণ:
import com.fasterxml.jackson.databind.ObjectMapper;
public record Product(String id, String name, double price) {}
public class RecordExample {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
// Serialization
Product product = new Product("P001", "Laptop", 799.99);
String json = mapper.writeValueAsString(product);
System.out.println("Serialized JSON: " + json);
// Deserialization
String inputJson = "{ \"id\": \"P001\", \"name\": \"Laptop\", \"price\": 799.99 }";
Product deserializedProduct = mapper.readValue(inputJson, Product.class);
System.out.println("Deserialized Product: ID = " + deserializedProduct.id() + ", Name = " + deserializedProduct.name());
}
}
Output:
Serialized JSON: {"id":"P001","name":"Laptop","price":799.99}
Deserialized Product: ID = P001, Name = Laptop
Constructor-based Injection vs Default Behavior
| বৈশিষ্ট্য | Setter-based Injection | Constructor-based Injection |
|---|---|---|
| মেমোরি ব্যবস্থাপনা | ফিল্ড আলাদাভাবে সেট হয়। | ইনস্ট্যান্স তৈরির সময় সব ফিল্ড সেট হয়। |
| Immutable Support | সরাসরি সমর্থন নেই। | Immutable ক্লাস সরাসরি সমর্থিত। |
| Jackson কনফিগারেশন | ডিফল্ট মডেল। | @JsonCreator এবং @JsonProperty প্রয়োজন। |
সাধারণ সমস্যা এবং সমাধান
১. "No Creators, like default constructor" Error
- Immutable Class-এ ডিফল্ট কনস্ট্রাক্টর না থাকলে Jackson ডেসিরিয়ালাইজ করতে পারে না।
- সমাধান:
@JsonCreatorএবং@JsonPropertyব্যবহার করুন।
২. Missing Default Constructor
- Jackson ডিফল্টভাবে ডিফল্ট কনস্ট্রাক্টর খোঁজে।
- সমাধান: Immutable Class-এর ক্ষেত্রে
@JsonCreatorব্যবহার বাধ্যতামূলক।
৩. Custom Field Mapping
- JSON ফিল্ডের নাম এবং কনস্ট্রাক্টরের প্যারামিটার নাম ভিন্ন হলে সমস্যা হয়।
- সমাধান:
@JsonPropertyদিয়ে ফিল্ড ম্যাপিং নিশ্চিত করুন।
- Immutable Class এবং Constructor-based Injection হ্যান্ডল করতে Jackson শক্তিশালী সমর্থন প্রদান করে।
@JsonCreatorএবং@JsonPropertyসঠিকভাবে ব্যবহার করলে Immutable ক্লাসের সাথে সহজে কাজ করা যায়।- Java Records এর জন্য Jackson এর বিল্ট-ইন সমর্থন উন্নত পারফরম্যান্স এবং কোডের সরলতা নিশ্চিত করে।
Read more