কম্পিউটার

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


The @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
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
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. জাভা - স্ট্রিং হিসাবে JSON ফাইল কীভাবে পড়তে হয়