কম্পিউটার টিউটোরিয়াল

জাভাতে @JsonCreator অ্যানোটেশন ব্যবহার করে JSON স্ট্রিং ডিসিরিয়ালাইজ কীভাবে করবেন?

@JsonProperty অ্যানোটেশন JSON-এ প্রপার্টি নাম নির্দেশ করতে ব্যবহৃত হয়। এই অ্যানোটেশন কনস্ট্রাক্টর বা ফ্যাক্টরি মেথডের জন্য ব্যবহার করা যায়। @JsonCreator অ্যানোটেশন ऐसी পরিস্থিতিতে উপকারী যেখানে @JsonSetter অ্যানোটেশন ব্যবহার করা যায় না। উদাহরণস্বরূপ, ইমিউটেবল অবজেক্টগুলোর কোনো সেটার মেথড থাকে না, তাই তাদের ইনিশিয়াল মান কনস্ট্রাক্টরের মাধ্যমে ইনজেক্ট করতে হয়।

@JsonProperty - কনস্ট্রাক্টর

উদাহরণ

import com.fasterxml.jackson.annotation.*; 
import java.io.IOException; 
import com.fasterxml.jackson.databind.*; 

public class JsonCreatorTest1 { 
    public static void main(String[] args) throws IOException { 
        ObjectMapper om = new ObjectMapper(); 
        String jsonString = "{\"id\":\"101\", \"fullname\":\"Ravi Chandra\", \"location\":\"Pune\"}"; 
        System.out.println("JSON: " + jsonString); 
        
        Customer customer = om.readValue(jsonString, Customer.class); 
        System.out.println(customer); 
    } 
} 

// Customer ক্লাস 
class Customer { 
    private String id; 
    private String name; 
    private String address; 
    
    public Customer() {} 
    
    @JsonCreator 
    public Customer( 
        @JsonProperty("id") String id, 
        @JsonProperty("fullname") String name, 
        @JsonProperty("location") String address) { 
        this.id = id; 
        this.name = name; 
        this.address = address; 
    } 
    
    @Override 
    public String toString() { 
        return "Customer [id=" + id + ", name=" + name + ", address=" + address + "]"; 
    } 
}

আউটপুট

JSON: {"id":"101", "fullname":"Ravi Chandra", "location":"Pune"} 
Customer [id=101, name=Ravi Chandra, address=Pune]

@JsonCreator - ফ্যাক্টরি মেথড

উদাহরণ

import com.fasterxml.jackson.annotation.*; 
import java.io.IOException; 
import com.fasterxml.jackson.databind.*; 

public class JsonCreatorTest2 { 
    public static void main(String[] args) throws IOException { 
        ObjectMapper mapper = new ObjectMapper(); 
        String jsonString = "{\"id\":\"102\", \"fullname\":\"Raja Ramesh\", \"location\":\"Hyderabad\"}"; 
        System.out.println("JSON: " + jsonString); 
        
        Customer customer = mapper.readValue(jsonString, Customer.class); 
        System.out.println(customer); 
    } 
} 

// Customer ক্লাস 
class Customer { 
    private String id; 
    private String name; 
    private String address; 
    
    public Customer() {} 
    
    @JsonCreator 
    public static Customer createCustomer( 
        @JsonProperty("id") String id, 
        @JsonProperty("fullname") String name, 
        @JsonProperty("location") String address) { 
        Customer customer = new Customer(); 
        customer.id = id; 
        customer.name = name; 
        customer.address = address; 
        return customer; 
    } 
    
    @Override 
    public String toString() { 
        return "Customer [id=" + id + ", name=" + name + ", address=" + address + "]"; 
    } 
}

আউটপুট

JSON: {"id":"101", "fullname":"Raja Ramesh", "location":"Hyderabad"} 
Customer [id=102, name=Raja Ramesh, address=Hyderabad]
  1. জাভাতে Gson লাইব্রেরি ব্যবহার করে JSON স্ট্রিং ফাইলে লেখার পদ্ধতি

  2. জাভাতে JSON ফাইলের কন্টেন্ট কীভাবে পড়বেন?

  3. পাইথনে স্ট্রিংকে JSON-এ রূপান্তর করা

  4. জাভা গাইড: API টেস্টিংয়ের জন্য JSON ফাইলকে স্ট্রিং-এ পড়া