Interface এবং Abstract Class এর জন্য Gson ব্যবহার

Gson এর Polymorphic Object হ্যান্ডলিং - জিসন (Gson) - Java Technologies

440

Gson এ Interface এবং Abstract Class এর জন্য সেরিয়ালাইজেশন (JSON-এ রূপান্তর) এবং ডেসিরিয়ালাইজেশন (JSON থেকে Java অবজেক্টে রূপান্তর) করতে কিছু বিশেষ কৌশল ব্যবহার করতে হয়, কারণ Gson ডিফল্টভাবে Interface বা Abstract Class কে সঠিকভাবে সেরিয়ালাইজ বা ডেসিরিয়ালাইজ করতে পারে না। আপনি TypeAdapter অথবা JsonAdapter ব্যবহার করে কাস্টম লজিক বাস্তবায়ন করতে পারেন।

উদাহরণ: Interface এবং Abstract Class এর জন্য Gson ব্যবহার

1. Abstract Class এবং Interface এর সেরিয়ালাইজেশন এবং ডেসিরিয়ালাইজেশন

ধরা যাক, আমাদের একটি Shape নামক abstract class রয়েছে এবং এটি দুইটি subclass: Circle এবং Rectangle দ্বারা এক্সটেন্ড করা হয়েছে। Shape ক্লাসটি একটি interface যা বিভিন্ন ধরনের শেপের জন্য ব্যবহৃত হবে।

এখন, আমাদের এই Shape ক্লাসের অবজেক্টকে JSON-এ রূপান্তর করতে এবং JSON থেকে Java অবজেক্টে রূপান্তর করতে হবে।

Solution 1: TypeAdapter ব্যবহার করে

এখানে Shape ইন্টারফেস বা অ্যাবস্ট্র্যাক্ট ক্লাসকে JSON-এ কনভার্ট করার জন্য কাস্টম TypeAdapter ব্যবহার করতে হবে, যাতে আমরা JSON-এ type ফিল্ড যোগ করতে পারি যা নির্দেশ করবে যে অবজেক্টটি কোন সাবক্লাসের।

উদাহরণ কোড:

import com.google.gson.*;
import java.lang.reflect.Type;

// Abstract Class
abstract class Shape {
    int x, y;
    abstract void draw();
}

// Subclass 1: Circle
class Circle extends Shape {
    int radius;

    public Circle(int x, int y, int radius) {
        this.x = x;
        this.y = y;
        this.radius = radius;
    }

    @Override
    void draw() {
        System.out.println("Drawing Circle");
    }
}

// Subclass 2: Rectangle
class Rectangle extends Shape {
    int width, height;

    public Rectangle(int x, int y, int width, int height) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }

    @Override
    void draw() {
        System.out.println("Drawing Rectangle");
    }
}

// TypeAdapter for Shape
class ShapeAdapter implements JsonSerializer<Shape>, JsonDeserializer<Shape> {

    @Override
    public JsonElement serialize(Shape src, Type typeOfSrc, JsonSerializationContext context) {
        JsonObject jsonObject = new JsonObject();
        
        // Adding type to identify subclass type
        if (src instanceof Circle) {
            jsonObject.addProperty("type", "Circle");
            jsonObject.add("data", context.serialize(src));
        } else if (src instanceof Rectangle) {
            jsonObject.addProperty("type", "Rectangle");
            jsonObject.add("data", context.serialize(src));
        }
        
        return jsonObject;
    }

    @Override
    public Shape deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
        JsonObject jsonObject = json.getAsJsonObject();
        String type = jsonObject.get("type").getAsString();

        Shape shape = null;
        if ("Circle".equals(type)) {
            shape = context.deserialize(jsonObject.get("data"), Circle.class);
        } else if ("Rectangle".equals(type)) {
            shape = context.deserialize(jsonObject.get("data"), Rectangle.class);
        }

        return shape;
    }
}

public class Main {
    public static void main(String[] args) {
        // Create a Circle object
        Shape circle = new Circle(10, 20, 15);
        Shape rectangle = new Rectangle(5, 5, 30, 40);

        // Create Gson with TypeAdapter
        Gson gson = new GsonBuilder()
                .registerTypeAdapter(Shape.class, new ShapeAdapter())
                .create();

        // Serialize Circle object to JSON
        String jsonCircle = gson.toJson(circle);
        System.out.println("Serialized Circle: " + jsonCircle);

        // Deserialize Circle JSON back to Java object
        Shape deserializedCircle = gson.fromJson(jsonCircle, Shape.class);
        System.out.println("Deserialized Circle: ");
        deserializedCircle.draw();

        // Serialize Rectangle object to JSON
        String jsonRectangle = gson.toJson(rectangle);
        System.out.println("Serialized Rectangle: " + jsonRectangle);

        // Deserialize Rectangle JSON back to Java object
        Shape deserializedRectangle = gson.fromJson(jsonRectangle, Shape.class);
        System.out.println("Deserialized Rectangle: ");
        deserializedRectangle.draw();
    }
}

আউটপুট:

Serialized Circle: {"type":"Circle","data":{"x":10,"y":20,"radius":15}}
Deserialized Circle: 
Drawing Circle
Serialized Rectangle: {"type":"Rectangle","data":{"x":5,"y":5,"width":30,"height":40}}
Deserialized Rectangle: 
Drawing Rectangle

ব্যাখ্যা:

  1. Shape একটি অ্যাবস্ট্র্যাক্ট ক্লাস যা Circle এবং Rectangle এর মতো subclass গুলোকে প্রতিনিধিত্ব করে।
  2. ShapeAdapter ক্লাসটি Shape অবজেক্টের জন্য কাস্টম সেরিয়ালাইজেশন এবং ডেসিরিয়ালাইজেশন লজিক প্রযোগ করে। এখানে আমরা type ফিল্ড যোগ করেছি যাতে Circle এবং Rectangle এর পার্থক্য করা যায়।
  3. Serialization: ShapeAdapter ব্যবহার করে Circle এবং Rectangle কে JSON এ রূপান্তর করা হয়েছে।
  4. Deserialization: JSON থেকে Shape অবজেক্টে রূপান্তর করা হয়েছে এবং JSON এর type ফিল্ড ব্যবহার করে সঠিক সাবক্লাসে রূপান্তর করা হয়েছে।

Solution 2: @JsonAdapter অ্যানোটেশন ব্যবহার করা

@JsonAdapter অ্যানোটেশনটি ক্লাস অথবা ফিল্ডের জন্য কাস্টম TypeAdapter যুক্ত করার জন্য ব্যবহৃত হয়। এটি উপরের কোডের মতো কাস্টম অ্যাডাপ্টার ব্যবহার করার জন্য সুবিধাজনক হতে পারে।

উদাহরণ:

import com.google.gson.annotations.JsonAdapter;

@JsonAdapter(ShapeAdapter.class)
abstract class Shape {
    int x, y;
    abstract void draw();
}

এখানে Shape ক্লাসের উপরে @JsonAdapter(ShapeAdapter.class) অ্যানোটেশন ব্যবহার করা হয়েছে, যা ShapeAdapter কে সেরিয়ালাইজেশন এবং ডেসিরিয়ালাইজেশন কাস্টম লজিক হিসেবে অ্যাসাইন করে।

  • Gson দিয়ে Interface এবং Abstract Class এর জন্য সেরিয়ালাইজেশন এবং ডেসিরিয়ালাইজেশন করতে হলে আপনাকে TypeAdapter অথবা JsonAdapter ব্যবহার করতে হবে।
  • আপনি কাস্টম সেরিয়ালাইজেশন এবং ডেসিরিয়ালাইজেশন লজিক তৈরি করে, সাবক্লাসের পার্থক্য নির্ধারণ এবং JSON ফিল্ডকে কাস্টমাইজ করতে পারেন।
  • TypeAdapter অথবা @JsonAdapter অ্যানোটেশন ব্যবহারের মাধ্যমে Interface এবং Abstract Class এর জন্য কাস্টম লজিক সহজেই বাস্তবায়ন করা সম্ভব।
Content added By
Promotion

Are you sure to start over?

Loading...