Jackson Serialization (Java Object থেকে JSON) এবং Deserialization (JSON থেকে Java Object) এর সময় বিভিন্ন ধরনের Exception ঘটতে পারে। এই exceptions গুলো invalid data format, missing required fields, type mismatches, এবং circular references সহ আরও অনেক কারণে হতে পারে।
Jackson-এ exception handling-কে কাস্টমাইজ করা যেতে পারে, যেমন @JsonProperty, @JsonDeserialize, @JsonSerialize, এবং @JsonIgnoreProperties অ্যানোটেশন ব্যবহার করে।
এছাড়া, Jackson-এর JsonProcessingException, JsonParseException, JsonMappingException এর মতো built-in exceptions ব্যবহৃত হয়, যা JSON-এ রূপান্তরের সময় সঠিকভাবে exception ট্র্যাক করতে সহায়তা করে।
Jackson Serialization এবং Deserialization Exception Handling:
১. JSON Parse Exceptions
যখন JSON স্ট্রিং-এ কোনো ত্রুটি থাকে বা তা অবৈধ (invalid) হয়, তখন JsonParseException বা JsonMappingException ঘটতে পারে।
উদাহরণ: Invalid JSON Format
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.JsonParseException;
public class JsonParseExceptionExample {
public static void main(String[] args) {
String invalidJson = "{name: 'John Doe', age: 30}"; // Invalid JSON (missing quotes around field names)
ObjectMapper objectMapper = new ObjectMapper();
try {
// Invalid JSON Parsing
objectMapper.readValue(invalidJson, Person.class);
} catch (JsonParseException e) {
System.out.println("Invalid JSON Format: " + e.getMessage());
} catch (Exception e) {
e.printStackTrace();
}
}
}
class Person {
private String name;
private int age;
// Getters and Setters
}
Output:
Invalid JSON Format: Unexpected character ('n' (code 110)): was expecting double-quote to start field name
- Explanation: এখানে, JSON স্ট্রিং-এ ফিল্ড নামের চারপাশে ডাবল কোটেশন মার্কস (quotes) নেই, তাই
JsonParseExceptionঘটছে।
২. Missing Required Fields (Deserialization Error)
যখন JSON থেকে একটি অবজেক্ট ডেসিরিয়ালাইজ করা হয় এবং কোনো প্রয়োজনীয় ফিল্ড অনুপস্থিত থাকে, তখন JsonMappingException ঘটতে পারে।
উদাহরণ: Missing Required Fields
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.JsonMappingException;
public class JsonMappingExceptionExample {
public static void main(String[] args) {
String json = "{\"name\":\"John Doe\"}"; // Missing age field
ObjectMapper objectMapper = new ObjectMapper();
try {
// Deserialization: Missing "age" field
Person person = objectMapper.readValue(json, Person.class);
} catch (JsonMappingException e) {
System.out.println("Missing required field: " + e.getMessage());
} catch (Exception e) {
e.printStackTrace();
}
}
}
class Person {
private String name;
private int age; // Age is required
// Getters and Setters
}
Output:
Missing required field: Can not construct instance of com.example.Person: no suitable constructor found, can not deserialize from Object value (no delegate- or property-based creator)
- Explanation: এখানে,
ageফিল্ড JSON-এ অনুপস্থিত, যাPersonক্লাসে required হিসেবে কনফিগার করা ছিল, ফলেJsonMappingExceptionঘটেছে।
৩. @JsonProperty দিয়ে Required Fields নির্ধারণ
আপনি @JsonProperty(required = true) ব্যবহার করে একটি ফিল্ডকে required হিসেবে চিহ্নিত করতে পারেন, যা Deserialization এর সময় ফিল্ড অনুপস্থিত থাকলে Exception throw করবে।
উদাহরণ:
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.JsonMappingException;
public class JsonPropertyRequiredExample {
public static void main(String[] args) {
String json = "{\"name\":\"John Doe\"}"; // Missing "age" field
ObjectMapper objectMapper = new ObjectMapper();
try {
// Deserialization with @JsonProperty(required = true)
Person person = objectMapper.readValue(json, Person.class);
} catch (JsonMappingException e) {
System.out.println("Required field missing: " + e.getMessage());
} catch (Exception e) {
e.printStackTrace();
}
}
}
class Person {
private String name;
@JsonProperty(required = true)
private int age; // Required field
// Getters and Setters
}
Output:
Required field missing: Missing required creator property 'age' (index 1)
- Explanation:
@JsonProperty(required = true)ব্যবহার করার কারণেageফিল্ডটি JSON থেকে অনুপস্থিত থাকলে exception ঘটবে।
৪. Circular References Handling (Avoiding Infinite Recursion)
Circular references JSON Serialization এবং Deserialization এর সময় সমস্যা সৃষ্টি করতে পারে। Jackson-এ @JsonManagedReference এবং @JsonBackReference ব্যবহার করে Circular references এড়ানো যায়।
উদাহরণ:
import com.fasterxml.jackson.annotation.JsonManagedReference;
import com.fasterxml.jackson.annotation.JsonBackReference;
public class Person {
private String name;
@JsonManagedReference
private Address address;
// Constructor, Getters, Setters
}
public class Address {
private String street;
@JsonBackReference
private Person person;
// Constructor, Getters, Setters
}
Serialization Example:
import com.fasterxml.jackson.databind.ObjectMapper;
public class CircularReferenceExample {
public static void main(String[] args) throws Exception {
Person person = new Person("John Doe", new Address("123 Elm St", null));
Address address = person.getAddress();
address.setPerson(person);
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(person);
System.out.println(json);
}
}
JSON Output:
{
"name": "John Doe",
"address": {
"street": "123 Elm St"
}
}
- Explanation:
@JsonManagedReferenceএবং@JsonBackReferenceব্যবহার করার ফলে Circular reference গুলো JSON Serialization থেকে বাদ দেওয়া হয়েছে।
5. Custom Exception Handling
Jackson এর মাধ্যমে কাস্টম Exception Handling করতে হলে আপনি নিজস্ব Exception class তৈরি করে Jackson এর JsonProcessingException এবং JsonMappingException ব্যবহার করতে পারেন।
উদাহরণ:
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.JsonProcessingException;
public class CustomExceptionExample {
public static void main(String[] args) {
String json = "{\"name\":\"John Doe\",\"age\":\"abc\"}"; // Invalid age format
ObjectMapper objectMapper = new ObjectMapper();
try {
// Deserialization with invalid data
Person person = objectMapper.readValue(json, Person.class);
} catch (JsonProcessingException e) {
System.out.println("Custom Error: Invalid data format: " + e.getMessage());
} catch (Exception e) {
e.printStackTrace();
}
}
}
class Person {
private String name;
private int age;
// Getters and Setters
}
Output:
Custom Error: Invalid data format: Cannot deserialize value of type `int` from String "abc": not a valid Integer value
- Exception Handling Jackson-এ গুরুত্বপূর্ণ একটি বিষয়। JSON থেকে Java Object এবং Java Object থেকে JSON-এ রূপান্তরের সময় বিভিন্ন ধরনের Exception ঘটতে পারে, যেমন invalid format, missing required fields, type mismatch, এবং Circular references।
- Jackson-এ
JsonParseException,JsonMappingException,JsonProcessingExceptionব্যবহার করে এই Exception গুলো ট্র্যাক করা যায় এবং কাস্টম Exception Handling বাস্তবায়ন করা যায়। @JsonProperty,@JsonManagedReference,@JsonBackReferenceইত্যাদি অ্যানোটেশন ব্যবহার করে আপনি Exception Handling আরও কাস্টমাইজ করতে পারেন।
Jackson লাইব্রেরি ব্যবহার করার সময় কিছু সাধারণ সমস্যা দেখা দিতে পারে, বিশেষ করে JSON Serialization এবং Deserialization এর প্রক্রিয়া চলাকালে। এই সমস্যা গুলো সাধারণত mapping issues, missing fields, invalid data types, বা circular references ইত্যাদি থেকে তৈরি হয়।
উপসংহার
Read more