Jackson-এর @JsonCreator এবং @JsonValue অ্যানোটেশন ব্যবহার করা হয় Serialization এবং Deserialization প্রক্রিয়া কাস্টমাইজ করতে। এরা JSON ডেটার সাথে Java Object-এর সিরিয়ালাইজেশন এবং ডেসিরিয়ালাইজেশনের নিয়ন্ত্রণ প্রদান করে।
১. @JsonCreator
@JsonCreator ডেসিরিয়ালাইজেশনের সময় একটি কাস্টম পদ্ধতি বা কনস্ট্রাক্টর ব্যবহার করার অনুমতি দেয়। এটি JSON ডেটা থেকে একটি Java Object তৈরি করার নিয়ম নির্ধারণ করে।
মূল বৈশিষ্ট্য:
- JSON ডেটার একটি কাস্টম ম্যাপিং তৈরি করতে ব্যবহৃত হয়।
- ডিফল্ট কনস্ট্রাক্টর ব্যবহার না করেও Object তৈরি করতে সক্ষম।
- কনস্ট্রাক্টর বা স্ট্যাটিক মেথডে প্রয়োগ করা যায়।
ব্যবহার:
Simple Example:
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
public class User {
private String name;
private int age;
@JsonCreator
public User(@JsonProperty("user_name") String name, @JsonProperty("user_age") int age) {
this.name = name;
this.age = age;
}
// Getters
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
JSON → Java Object (Deserialization):
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonCreatorExample {
public static void main(String[] args) throws Exception {
String json = "{\"user_name\":\"John Doe\",\"user_age\":30}";
ObjectMapper objectMapper = new ObjectMapper();
User user = objectMapper.readValue(json, User.class);
System.out.println("Name: " + user.getName() + ", Age: " + user.getAge());
}
}
Output:
Name: John Doe, Age: 30
Static Factory Method Example:
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
public class User {
private String name;
private int age;
private User(String name, int age) {
this.name = name;
this.age = age;
}
@JsonCreator
public static User createUser(@JsonProperty("name") String name, @JsonProperty("age") int age) {
return new User(name, age);
}
// Getters
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
২. @JsonValue
@JsonValue সিরিয়ালাইজেশনের সময় একটি Object-কে JSON-এ কিভাবে রূপান্তরিত করা হবে তা কাস্টমাইজ করে। এটি একটি Object থেকে সরাসরি একটি প্রপার্টি বা ভ্যালু JSON হিসেবে ব্যবহার করার অনুমতি দেয়।
মূল বৈশিষ্ট্য:
- Java Object → JSON সিরিয়ালাইজেশনে নিয়ন্ত্রণ প্রদান করে।
- একটি ফিল্ড, মেথড, বা কনভার্টেড মান সরাসরি JSON ভ্যালু হিসেবে সেট করতে ব্যবহৃত হয়।
- একাধিক ফিল্ডের পরিবর্তে শুধুমাত্র একটি ফিল্ড বা মান JSON হিসেবে রিটার্ন করে।
ব্যবহার:
Simple Example:
import com.fasterxml.jackson.annotation.JsonValue;
public class User {
private String name;
public User(String name) {
this.name = name;
}
@JsonValue
public String getName() {
return name;
}
}
Java Object → JSON (Serialization):
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonValueExample {
public static void main(String[] args) throws Exception {
User user = new User("John Doe");
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(user);
System.out.println(json);
}
}
Output:
"John Doe"
Complex Example:
import com.fasterxml.jackson.annotation.JsonValue;
public class User {
private String name;
private int age;
public User(String name, int age) {
this.name = name;
this.age = age;
}
@JsonValue
public String toJson() {
return name + " (" + age + ")";
}
}
Serialization Output:
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonValueExample {
public static void main(String[] args) throws Exception {
User user = new User("John Doe", 30);
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(user);
System.out.println(json);
}
}
Output:
"John Doe (30)"
পার্থক্য: @JsonCreator বনাম @JsonValue
| বৈশিষ্ট্য | @JsonCreator | @JsonValue |
|---|---|---|
| মূল কাজ | JSON থেকে Java Object তৈরি করতে ব্যবহার হয়। | Java Object থেকে JSON সিরিয়ালাইজেশনের নিয়ন্ত্রণ। |
| ব্যবহারক্ষেত্র | ডেসিরিয়ালাইজেশনের জন্য। | সিরিয়ালাইজেশনের জন্য। |
| প্রভাবিত ফাংশন | কনস্ট্রাক্টর বা স্ট্যাটিক মেথড। | মেথড বা ফিল্ড। |
| উদাহরণ | JSON ফিল্ড কাস্টম ম্যাপিং। | একটি Object-এর নির্দিষ্ট মান JSON হিসেবে রিটার্ন। |
সমন্বিত উদাহরণ
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonValue;
public class User {
private String name;
private int age;
@JsonCreator
public User(@JsonProperty("user_name") String name, @JsonProperty("user_age") int age) {
this.name = name;
this.age = age;
}
@JsonValue
public String toJson() {
return name + " (" + age + ")";
}
}
Serialization এবং Deserialization:
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonCreatorValueExample {
public static void main(String[] args) throws Exception {
// Deserialization
String json = "{\"user_name\":\"John Doe\",\"user_age\":30}";
ObjectMapper objectMapper = new ObjectMapper();
User user = objectMapper.readValue(json, User.class);
// Serialization
String serializedJson = objectMapper.writeValueAsString(user);
System.out.println("Deserialized User: " + user.toJson());
System.out.println("Serialized JSON: " + serializedJson);
}
}
Output:
Deserialized User: John Doe (30)
Serialized JSON: "John Doe (30)"
@JsonCreator: ডেসিরিয়ালাইজেশনের সময় JSON ডেটাকে Java Object-এ কাস্টম পদ্ধতিতে রূপান্তরিত করতে ব্যবহৃত হয়।@JsonValue: সিরিয়ালাইজেশনের সময় Object-কে নির্দিষ্ট ফরম্যাটে JSON হিসেবে রূপান্তরিত করতে ব্যবহৃত হয়।- ব্যবহার ক্ষেত্র: REST API ডেভেলপমেন্ট এবং JSON ডেটার কাস্টম ম্যানিপুলেশনে অত্যন্ত কার্যকর।
Jackson এর @JsonCreator অ্যানোটেশন ব্যবহার করে JSON Deserialization (JSON থেকে Java Object এ রূপান্তর) এর সময় একটি কাস্টম কনস্ট্রাক্টর ব্যবহার করা যায়। এটি বিশেষত তখন উপযোগী যখন একটি ক্লাসে ফিল্ডগুলো ফাইনাল থাকে বা ফিল্ডগুলোকে সরাসরি Constructor এর মাধ্যমে ইনিশিয়ালাইজ করতে হয়।
@JsonCreator এর কাজ
@JsonCreatorব্যবহার করে Jackson কে জানানো হয় যে এটি কোন কনস্ট্রাক্টর বা ফ্যাক্টরি মেথড JSON থেকে Java Object তৈরি করার জন্য ব্যবহার করবে।@JsonPropertyদিয়ে ফিল্ডগুলোর নাম এবং তাদের ম্যাপিং নির্দিষ্ট করা হয়।
Example: Custom Constructor
Step 1: মডেল ক্লাস তৈরি করা
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
public class User {
private final String name;
private final int age;
private final String email;
// Custom Constructor
@JsonCreator
public User(
@JsonProperty("name") String name,
@JsonProperty("age") int age,
@JsonProperty("email") String email) {
this.name = name;
this.age = age;
this.email = email;
}
// Getters
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getEmail() {
return email;
}
}
Step 2: JSON Deserialization
Sample JSON:
{
"name": "Alice",
"age": 30,
"email": "alice@example.com"
}
Deserialization Code:
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonCreatorExample {
public static void main(String[] args) throws Exception {
String json = "{\"name\":\"Alice\",\"age\":30,\"email\":\"alice@example.com\"}";
ObjectMapper mapper = new ObjectMapper();
// JSON থেকে User Object তৈরি
User user = mapper.readValue(json, User.class);
// Output
System.out.println("Name: " + user.getName());
System.out.println("Age: " + user.getAge());
System.out.println("Email: " + user.getEmail());
}
}
Output:
Name: Alice
Age: 30
Email: alice@example.com
Factory Method এর জন্য @JsonCreator
@JsonCreator ফ্যাক্টরি মেথডের সাথেও কাজ করে।
Step 1: মডেল ক্লাস
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
public class User {
private final String name;
private final int age;
private final String email;
private User(String name, int age, String email) {
this.name = name;
this.age = age;
this.email = email;
}
// Factory Method
@JsonCreator
public static User createUser(
@JsonProperty("name") String name,
@JsonProperty("age") int age,
@JsonProperty("email") String email) {
return new User(name, age, email);
}
// Getters
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getEmail() {
return email;
}
}
Step 2: JSON থেকে Object তৈরি
String json = "{\"name\":\"Bob\",\"age\":25,\"email\":\"bob@example.com\"}";
User user = mapper.readValue(json, User.class);
System.out.println("Name: " + user.getName());
System.out.println("Age: " + user.getAge());
System.out.println("Email: " + user.getEmail());
Field Order Independent Mapping
@JsonCreator ফিল্ডের অর্ডার-ইনডিপেনডেন্ট ম্যাপিং নিশ্চিত করে।
JSON:
{
"age": 40,
"email": "john@example.com",
"name": "John"
}
Deserialization Code:
User user = mapper.readValue(json, User.class);
System.out.println("Name: " + user.getName());
System.out.println("Age: " + user.getAge());
System.out.println("Email: " + user.getEmail());
Output:
Name: John
Age: 40
Email: john@example.com
নেস্টেড অবজেক্ট সহ @JsonCreator
Step 1: Address Class
public class Address {
private final String city;
private final String zip;
@JsonCreator
public Address(
@JsonProperty("city") String city,
@JsonProperty("zip") String zip) {
this.city = city;
this.zip = zip;
}
public String getCity() {
return city;
}
public String getZip() {
return zip;
}
}
Step 2: User Class
public class User {
private final String name;
private final Address address;
@JsonCreator
public User(
@JsonProperty("name") String name,
@JsonProperty("address") Address address) {
this.name = name;
this.address = address;
}
public String getName() {
return name;
}
public Address getAddress() {
return address;
}
}
Step 3: JSON Input
{
"name": "Alice",
"address": {
"city": "New York",
"zip": "10001"
}
}
Step 4: Deserialization
String json = "{\"name\":\"Alice\",\"address\":{\"city\":\"New York\",\"zip\":\"10001\"}}";
User user = mapper.readValue(json, User.class);
System.out.println("Name: " + user.getName());
System.out.println("City: " + user.getAddress().getCity());
System.out.println("Zip: " + user.getAddress().getZip());
Output:
Name: Alice
City: New York
Zip: 10001
@JsonCreatorJSON থেকে Object তৈরি করার জন্য কাস্টম Constructor বা Factory Method নির্দিষ্ট করতে ব্যবহার করা হয়।@JsonPropertyদিয়ে ফিল্ডের নাম এবং JSON এর সাথে তাদের ম্যাপিং নিশ্চিত করা হয়।- এটি বিশেষভাবে কার্যকর যখন ফিল্ডগুলো ফাইনাল থাকে বা ক্লাস Immutable হয়।
@JsonCreatorফিল্ড অর্ডার ইনডিপেনডেন্ট এবং নেস্টেড JSON এর সাথেও কাজ করে।
Jackson JSON থেকে Java Object তৈরি করার সময় Multiple Constructors থাকলে কোন কনস্ট্রাক্টর ব্যবহার করা হবে তা Jackson Annotations দিয়ে নির্ধারণ করা যায়।
1. Default Behavior
Jackson ডিফল্টভাবে নো-আর্গ কনস্ট্রাক্টর (No-Args Constructor) ব্যবহার করে। যদি নো-আর্গ কনস্ট্রাক্টর না থাকে, তবে এটি ব্যর্থ হবে।
Example Without Annotations:
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"
}
""";
ObjectMapper objectMapper = new ObjectMapper();
User user = objectMapper.readValue(json, User.class);
System.out.println("User Name: " + user.name);
}
}
class User {
public int id;
public String name;
public String email;
// Constructor 1
public User() {} // Default constructor
// Constructor 2
public User(int id, String name, String email) {
this.id = id;
this.name = name;
this.email = email;
}
}
আউটপুট:
User Name: John Doe
2. Multiple Constructors Handling
যদি একাধিক কনস্ট্রাক্টর থাকে, তখন Jackson বুঝবে না কোনটি ব্যবহার করতে হবে। এটি সমাধান করতে @JsonCreator এবং @JsonProperty ব্যবহার করা হয়।
3. @JsonCreator দিয়ে Constructor নির্বাচন
@JsonCreator অ্যানোটেশন একটি নির্দিষ্ট কনস্ট্রাক্টরকে JSON ডেসিরিয়ালাইজেশনের জন্য নির্বাচন করে।
Example with @JsonCreator:
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
class User {
public int id;
public String name;
public String email;
// Constructor 1 (Default)
public User() {}
// Constructor 2 (Selected for JSON Mapping)
@JsonCreator
public User(@JsonProperty("id") int id,
@JsonProperty("name") String name,
@JsonProperty("email") String email) {
this.id = id;
this.name = name;
this.email = email;
}
}
Deserialization Example:
public class Main {
public static void main(String[] args) throws Exception {
String json = """
{
"id": 101,
"name": "John Doe",
"email": "john.doe@example.com"
}
""";
ObjectMapper objectMapper = new ObjectMapper();
User user = objectMapper.readValue(json, User.class);
System.out.println("User Name: " + user.name);
}
}
আউটপুট:
User Name: John Doe
4. Complex Example: Multiple Constructors
মডেল ক্লাস:
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
class Product {
public int id;
public String name;
public double price;
// Default Constructor
public Product() {}
// Constructor 1
@JsonCreator
public Product(@JsonProperty("id") int id,
@JsonProperty("name") String name) {
this.id = id;
this.name = name;
}
// Constructor 2
public Product(int id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
}
Deserialization Example:
public class Main {
public static void main(String[] args) throws Exception {
String json = """
{
"id": 101,
"name": "Laptop"
}
""";
ObjectMapper objectMapper = new ObjectMapper();
Product product = objectMapper.readValue(json, Product.class);
System.out.println("Product Name: " + product.name);
System.out.println("Product Price: " + product.price); // Will remain 0.0
}
}
আউটপুট:
Product Name: Laptop
Product Price: 0.0
5. @JsonProperty দিয়ে Field Mapping নিশ্চিত করা
@JsonProperty ব্যবহার করে JSON ফিল্ড এবং কনস্ট্রাক্টরের প্যারামিটারগুলোর মধ্যে সুনির্দিষ্ট ম্যাপিং নিশ্চিত করা হয়।
Example:
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
class Employee {
public int employeeId;
public String employeeName;
@JsonCreator
public Employee(@JsonProperty("id") int employeeId,
@JsonProperty("name") String employeeName) {
this.employeeId = employeeId;
this.employeeName = employeeName;
}
}
Deserialization Example:
public class Main {
public static void main(String[] args) throws Exception {
String json = """
{
"id": 201,
"name": "Alice"
}
""";
ObjectMapper objectMapper = new ObjectMapper();
Employee employee = objectMapper.readValue(json, Employee.class);
System.out.println("Employee ID: " + employee.employeeId);
System.out.println("Employee Name: " + employee.employeeName);
}
}
আউটপুট:
Employee ID: 201
Employee Name: Alice
6. Lombok এবং Jackson একসাথে ব্যবহার
যদি আপনি Lombok ব্যবহার করেন, তাহলে @AllArgsConstructor বা @RequiredArgsConstructor এর সঙ্গে Jackson এর অ্যানোটেশন ব্যবহার করতে পারেন।
Example with Lombok:
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor(onConstructor = @__(@JsonCreator))
public class Department {
@JsonProperty("id")
private int id;
@JsonProperty("name")
private String name;
}
Deserialization Example:
public class Main {
public static void main(String[] args) throws Exception {
String json = """
{
"id": 501,
"name": "HR"
}
""";
ObjectMapper objectMapper = new ObjectMapper();
Department department = objectMapper.readValue(json, Department.class);
System.out.println("Department ID: " + department.getId());
System.out.println("Department Name: " + department.getName());
}
}
আউটপুট:
Department ID: 501
Department Name: HR
Key Points
- Multiple Constructors হ্যান্ডল করা:
- @JsonCreator ব্যবহার করে নির্দিষ্ট Constructor নির্বাচন করুন।
- @JsonProperty ব্যবহার করে JSON ফিল্ড এবং Constructor প্যারামিটার ম্যাপিং নিশ্চিত করুন।
- Default Constructor প্রয়োজনীয়:
- Jackson ডিফল্টভাবে নো-আর্গ কনস্ট্রাক্টর ব্যবহার করে, এটি না থাকলে @JsonCreator দিয়ে নির্দেশ করতে হবে।
- Lombok Integration:
- Lombok এবং Jackson একত্রে ব্যবহার করা হলে কনস্ট্রাক্টর অ্যানোটেশন সঠিকভাবে প্রয়োগ করুন।
এই পদ্ধতিগুলো অনুসরণ করে আপনি Jackson-এ Multiple Constructors সহ JSON Mapping সফলভাবে হ্যান্ডল করতে পারবেন।
@JsonValue অ্যানোটেশনটি Jackson-এ ব্যবহার করা হয় একটি Java Object থেকে single value serialization করার জন্য। সাধারণত, এটি তখন ব্যবহৃত হয় যখন কোনো Object কে JSON-এ serialize করার সময় কেবল একটি নির্দিষ্ট ফিল্ড বা ভ্যালু JSON আউটপুটে অন্তর্ভুক্ত করতে হয়।
1. @JsonValue কীভাবে কাজ করে?
@JsonValue একটি মেথড বা ফিল্ডের উপরে প্রয়োগ করা হয় যা JSON serialization-এর সময় Object-এর প্রতিনিধিত্ব করবে।
প্রযুক্তিগত বৈশিষ্ট্য:
- একাধিক ফিল্ডের পরিবর্তে একটি নির্দিষ্ট ভ্যালু JSON আউটপুটে অন্তর্ভুক্ত করবে।
- এটি serialization-এর সময় কার্যকর।
- ডিফল্টভাবে
@JsonValueএকটি ক্লাসে শুধুমাত্র একটি মেথডে ব্যবহার করা যেতে পারে।
2. Maven Dependency
Ensure your project has the required Jackson dependency in pom.xml:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.15.2</version>
</dependency>
3. Example of @JsonValue for Single Value Serialization
Basic Example:
import com.fasterxml.jackson.annotation.JsonValue;
public class Product {
private int id;
private String name;
public Product(int id, String name) {
this.id = id;
this.name = name;
}
@JsonValue
public String getName() {
return name;
}
// Getters and Setters
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNameValue() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Serialization Example:
import com.fasterxml.jackson.databind.ObjectMapper;
public class Main {
public static void main(String[] args) throws Exception {
Product product = new Product(1, "Smartphone");
ObjectMapper mapper = new ObjectMapper();
// Serialize the object
String json = mapper.writeValueAsString(product);
System.out.println("Serialized JSON: " + json);
}
}
Output:
"Smartphone"
4. Complex Example with Enum
@JsonValue খুবই কার্যকর যখন Enums থেকে কাস্টম ভ্যালু serialize করতে হয়।
Example:
import com.fasterxml.jackson.annotation.JsonValue;
public enum Status {
ACTIVE("active"),
INACTIVE("inactive"),
PENDING("pending");
private final String value;
Status(String value) {
this.value = value;
}
@JsonValue
public String getValue() {
return value;
}
}
Serialization:
import com.fasterxml.jackson.databind.ObjectMapper;
public class Main {
public static void main(String[] args) throws Exception {
Status status = Status.ACTIVE;
ObjectMapper mapper = new ObjectMapper();
// Serialize the enum
String json = mapper.writeValueAsString(status);
System.out.println("Serialized JSON: " + json);
}
}
Output:
"active"
5. Using @JsonValue in a Custom Class
Example: Custom Wrapper Class
import com.fasterxml.jackson.annotation.JsonValue;
public class Price {
private double amount;
public Price(double amount) {
this.amount = amount;
}
@JsonValue
public String toJson() {
return String.format("$%.2f", amount);
}
// Getters and Setters
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
}
Serialization:
import com.fasterxml.jackson.databind.ObjectMapper;
public class Main {
public static void main(String[] args) throws Exception {
Price price = new Price(120.5);
ObjectMapper mapper = new ObjectMapper();
// Serialize the price object
String json = mapper.writeValueAsString(price);
System.out.println("Serialized JSON: " + json);
}
}
Output:
"$120.50"
6. Combining @JsonValue with Other Annotations
@JsonValue কাস্টমাইজেশন করার সময় @JsonIgnore বা @JsonProperty এর সাথে ব্যবহার করা যায়।
Example:
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonValue;
public class Product {
private int id;
@JsonIgnore
private String name;
public Product(int id, String name) {
this.id = id;
this.name = name;
}
@JsonValue
public String toJsonValue() {
return String.format("Product{name='%s'}", name);
}
// Getters and Setters
}
Serialization:
import com.fasterxml.jackson.databind.ObjectMapper;
public class Main {
public static void main(String[] args) throws Exception {
Product product = new Product(1, "Smartphone");
ObjectMapper mapper = new ObjectMapper();
// Serialize the product object
String json = mapper.writeValueAsString(product);
System.out.println("Serialized JSON: " + json);
}
}
Output:
"Product{name='Smartphone'}"
7. Key Points to Remember
- Single Value Representation:
@JsonValueএকটি Object-এর single value serialization নিশ্চিত করে। - Unique Usage: একটি ক্লাসে শুধুমাত্র একটি
@JsonValueমেথড থাকতে পারে। - Enums Integration: Enums থেকে কাস্টম JSON ফিল্ড মান serialize করতে
@JsonValueখুবই কার্যকর। - Serialization Only:
@JsonValueশুধুমাত্র serialization-এর সময় কার্যকর; deserialization-এর জন্য এটি কাজ করে না।
8. Use Cases
- Enums: Custom string values serialize করতে।
- Wrapper Classes: Complex object-কে single string বা numeric value-তে serialize করতে।
- Custom Output: Object-এর নির্দিষ্ট ফিল্ড serialize করার জন্য।
@JsonValue একটি কার্যকর অ্যানোটেশন যা JSON serialization-এর সময় নির্দিষ্ট ভ্যালু অন্তর্ভুক্ত করতে দেয়। এটি বিশেষত Enums এবং Wrapper Classes-এ ব্যবহার করলে API-এর আউটপুট সহজ এবং অর্থপূর্ণ করে তোলে।
Jackson-এর @JsonCreator এবং @JsonValue অ্যানোটেশনগুলো JSON সিরিয়ালাইজ এবং ডি-সিরিয়ালাইজেশনে আরও বেশি নিয়ন্ত্রণ প্রদান করে। এগুলো সাধারণত কাস্টম অবজেক্ট তৈরির সময় বা JSON ফর্ম্যাট কাস্টমাইজ করার জন্য ব্যবহার করা হয়।
1. @JsonCreator
@JsonCreator ব্যবহার করা হয় ডি-সিরিয়ালাইজেশনের সময় একটি কাস্টম কন্সট্রাক্টর বা স্ট্যাটিক মেথড ব্যবহার করে JSON ডেটা থেকে একটি অবজেক্ট তৈরি করতে। এটি তখন ব্যবহৃত হয় যখন আপনার ক্লাসের ডিফল্ট কন্সট্রাক্টর না থাকে অথবা কাস্টম লজিক প্রয়োজন হয়।
ব্যবহার উদাহরণ:
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonCreatorExample {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
// JSON String
String json = "{\"id\":101,\"name\":\"John Doe\"}";
// Deserialize JSON to Java object
Employee employee = mapper.readValue(json, Employee.class);
System.out.println("Deserialized Employee: " + employee);
}
}
class Employee {
private int id;
private String name;
// Constructor with @JsonCreator
@JsonCreator
public Employee(@JsonProperty("id") int id, @JsonProperty("name") String name) {
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "Employee{id=" + id + ", name='" + name + "'}";
}
}
Output:
Deserialized Employee: Employee{id=101, name='John Doe'}
2. @JsonValue
@JsonValue ব্যবহার করা হয় সিরিয়ালাইজেশনের সময় একটি অবজেক্টকে JSON-এ রূপান্তর করার জন্য। এটি ফিল্ড, মেথড, বা প্রোপার্টিকে নির্দেশ করে যে সেটি JSON এর প্রতিনিধিত্ব করবে।
ব্যবহার উদাহরণ:
import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonValueExample {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
// Create an Employee object
Employee employee = new Employee(101, "John Doe");
// Serialize Employee object to JSON
String json = mapper.writeValueAsString(employee);
System.out.println("Serialized JSON: " + json);
}
}
class Employee {
private int id;
private String name;
public Employee(int id, String name) {
this.id = id;
this.name = name;
}
@JsonValue
public String toJson() {
return "Employee{id=" + id + ", name='" + name + "'}";
}
}
Output:
Serialized JSON: "Employee{id=101, name='John Doe'}"
উন্নত উদাহরণ: @JsonCreator এবং @JsonValue একসাথে
Enum হ্যান্ডল করার জন্য:
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.databind.ObjectMapper;
public class EnumExample {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
// Serialize Enum to JSON
String json = mapper.writeValueAsString(Status.ACTIVE);
System.out.println("Serialized Enum: " + json);
// Deserialize JSON to Enum
Status status = mapper.readValue("\"active\"", Status.class);
System.out.println("Deserialized Enum: " + status);
}
}
enum Status {
ACTIVE("active"),
INACTIVE("inactive");
private final String value;
Status(String value) {
this.value = value;
}
@JsonValue
public String getValue() {
return value;
}
@JsonCreator
public static Status fromValue(String value) {
for (Status status : Status.values()) {
if (status.value.equals(value)) {
return status;
}
}
throw new IllegalArgumentException("Unknown value: " + value);
}
}
Output:
Serialized Enum: "active"
Deserialized Enum: ACTIVE
@JsonCreator এবং @JsonValue এর সুবিধা
- @JsonCreator:
- JSON ডি-সিরিয়ালাইজেশনের সময় কাস্টম কন্সট্রাক্টর বা স্ট্যাটিক ফ্যাক্টরি মেথড ব্যবহার করতে সাহায্য করে।
- ইনপুট JSON-এর কাঠামো কাস্টমাইজ করার ক্ষমতা দেয়।
- @JsonValue:
- সিরিয়ালাইজেশনের সময় একটি নির্দিষ্ট ফিল্ড বা মেথডকে JSON এর জন্য একমাত্র প্রতিনিধিত্ব হিসেবে ব্যবহার করতে সাহায্য করে।
- Enum অথবা কাস্টম অবজেক্ট সিরিয়ালাইজ করার সময় কার্যকর।
- @JsonCreator: ডি-সিরিয়ালাইজ করার সময় নির্দিষ্ট কন্সট্রাক্টর বা মেথড ব্যবহার করতে।
- @JsonValue: সিরিয়ালাইজ করার সময় নির্দিষ্ট মেথড বা ফিল্ডকে JSON-এ রূপান্তর করতে।
এই দুটি অ্যানোটেশন জটিল JSON মডেল বা কাস্টম JSON ফরম্যাটের জন্য কার্যকর এবং নমনীয় সমাধান প্রদান করে।
Read more