@JsonProperty একটি Jackson অ্যানোটেশন যা JSON এবং Java Object এর ফিল্ডগুলির মধ্যে কাস্টম নাম বা ম্যাপিং সম্পর্ক তৈরি করতে ব্যবহৃত হয়। এটি মূলত JSON ডেটার প্রপার্টি এবং Java ফিল্ডের মধ্যে পারস্পরিক নাম ভিন্ন থাকলে সেগুলিকে সংযোগ করার জন্য ব্যবহার করা হয়।
@JsonProperty এর কার্যকারিতা
- JSON প্রপার্টি এবং Java ফিল্ডের মধ্যে কাস্টম নামকরণ।
- JSON সিরিয়ালাইজেশন এবং ডেসিরিয়ালাইজেশনে নির্দিষ্ট ফিল্ড ম্যাপিং।
- ডেসিরিয়ালাইজেশনের সময় প্রয়োজনীয় ফিল্ড নির্ধারণ করা।
@JsonProperty এর Practical উদাহরণ
১. কাস্টম নাম ম্যাপিং
import com.fasterxml.jackson.annotation.JsonProperty;
public class User {
@JsonProperty("user_id")
private int id;
@JsonProperty("user_name")
private String name;
@JsonProperty("user_email")
private String email;
// Getters and Setters
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
}
JSON Input:
{
"user_id": 1,
"user_name": "John Doe",
"user_email": "john.doe@example.com"
}
Usage Example:
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonPropertyExample {
public static void main(String[] args) throws Exception {
String json = "{ \"user_id\": 1, \"user_name\": \"John Doe\", \"user_email\": \"john.doe@example.com\" }";
ObjectMapper objectMapper = new ObjectMapper();
// JSON থেকে Object এ রূপান্তর
User user = objectMapper.readValue(json, User.class);
System.out.println("User Name: " + user.getName());
// Object থেকে JSON এ রূপান্তর
String serializedJson = objectMapper.writeValueAsString(user);
System.out.println("Serialized JSON: " + serializedJson);
}
}
আউটপুট:
User Name: John Doe
Serialized JSON: {"user_id":1,"user_name":"John Doe","user_email":"john.doe@example.com"}
২. ফিল্ড রেনেমিং
যদি JSON প্রপার্টির নাম Java ফিল্ডের নামের সাথে মেলে না, তবে @JsonProperty ব্যবহার করা হয়।
public class Employee {
@JsonProperty("empId")
private int id;
@JsonProperty("empName")
private String name;
@JsonProperty("department")
private String dept;
// Getters and Setters
}
JSON Input:
{
"empId": 101,
"empName": "Alice",
"department": "Engineering"
}
JSON Output:
{
"empId": 101,
"empName": "Alice",
"department": "Engineering"
}
৩. JSON সিরিয়ালাইজেশন এবং ডেসিরিয়ালাইজেশনে অর্ডার কাস্টমাইজ করা
@JsonProperty এর index অ্যাট্রিবিউট ব্যবহার করে সিরিয়ালাইজেশনের সময় JSON প্রপার্টির ক্রম নির্ধারণ করা যায়।
import com.fasterxml.jackson.annotation.JsonProperty;
public class Product {
@JsonProperty(value = "product_id", index = 1)
private int id;
@JsonProperty(value = "product_name", index = 2)
private String name;
@JsonProperty(value = "product_price", index = 3)
private double price;
// Getters and Setters
}
Usage Example:
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonPropertyOrderExample {
public static void main(String[] args) throws Exception {
Product product = new Product();
product.setId(101);
product.setName("Laptop");
product.setPrice(750.50);
ObjectMapper objectMapper = new ObjectMapper();
// Object থেকে JSON
String json = objectMapper.writeValueAsString(product);
System.out.println("Serialized JSON: " + json);
}
}
JSON Output:
{
"product_id": 101,
"product_name": "Laptop",
"product_price": 750.5
}
৪. ফিল্ডকে "required" হিসেবে চিহ্নিত করা
JSON ডেসিরিয়ালাইজেশনের সময় ফিল্ড প্রয়োজনীয় কিনা তা নির্ধারণ করতে required অ্যাট্রিবিউট ব্যবহার করা যায়।
import com.fasterxml.jackson.annotation.JsonProperty;
public class Account {
@JsonProperty(value = "account_id", required = true)
private int id;
@JsonProperty("account_name")
private String name;
// Getters and Setters
}
Usage Example:
public class JsonPropertyRequiredExample {
public static void main(String[] args) {
String json = "{ \"account_name\": \"Savings\" }"; // Missing account_id
ObjectMapper objectMapper = new ObjectMapper();
try {
Account account = objectMapper.readValue(json, Account.class);
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
}
Error Output:
Error: Missing required creator property 'account_id' (index 0)
@JsonProperty ব্যবহার করার মাধ্যমে:
- JSON এবং Java ফিল্ডের মধ্যে কাস্টম নামকরণ বা ম্যাপিং করা যায়।
- প্রপার্টি অর্ডার, রেনেমিং এবং প্রয়োজনীয় ফিল্ড নির্ধারণ করা সহজ হয়।
- এটি JSON ডেটা প্রসেসিংকে আরও নমনীয় ও কার্যকর করে তোলে।
উপযুক্ত ক্ষেত্রে: RESTful API-এর মাধ্যমে JSON ডেটা আদান-প্রদান এবং কাস্টম ডেটা ফরম্যাটিং নিশ্চিত করার জন্য এটি অত্যন্ত গুরুত্বপূর্ণ।
Read more