@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-এর আউটপুট সহজ এবং অর্থপূর্ণ করে তোলে।
Read more