Common Mistakes এবং তাদের সমাধান

Gson এর Best Practices এবং Common Pitfalls - জিসন (Gson) - Java Technologies

347

Gson ব্যবহার করার সময় কিছু সাধারণ ভুল হতে পারে, যা পারফরম্যান্স, ডেসিরিয়ালাইজেশন বা সিরিয়ালাইজেশন সম্পর্কিত ত্রুটি সৃষ্টি করতে পারে। এই ভুলগুলো সাধারণত JSON ফরম্যাটের সমস্যা, ডাটা টাইপের অসামঞ্জস্যতা বা সঠিকভাবে ফিল্ড নামের ম্যাপিং না হওয়া থেকে হয়। এই ধরনের সমস্যাগুলির সমাধান এবং প্রতিকার সম্পর্কে আলোচনা করা হলো:

1. Incorrect JSON Format

সমস্যা: JSON স্ট্রিংটি সঠিক ফরম্যাটে না থাকলে JsonSyntaxException বা JsonParseException ত্রুটি ঘটতে পারে। সাধারণত, অতিরিক্ত কমা, অবৈধ কোটেশন মার্ক, ভুলভাবে ওপেন বা ক্লোজড ব্রেস ইত্যাদি JSON সিনট্যাক্সের ত্রুটি সৃষ্টি করে।

উদাহরণ:

String invalidJson = "{ \"name\": \"John\", \"age\": 30, }"; // Extra comma
Gson gson = new Gson();
Person person = gson.fromJson(invalidJson, Person.class);  // Throws JsonSyntaxException

সমাধান: JSON ফরম্যাট সঠিকভাবে পরীক্ষা করতে হবে এবং অবাঞ্ছিত কমা বা ভুল কোটেশন মার্ক ইত্যাদি এড়িয়ে চলতে হবে।

  • JSON Validator: JSON স্ট্রিংটি ফর্ম্যাট সঠিক কিনা তা যাচাই করতে JSON লিন্টার বা Validator ব্যবহার করুন (যেমন jsonlint.com)।

2. Mismatch Between JSON and Java Object Field Names

সমস্যা: Gson ডিফল্টভাবে Java ক্লাসের ফিল্ড নামের সাথে JSON ফিল্ড নামের মিল খুঁজে। তবে, JSON ফিল্ড নাম এবং Java ফিল্ড নামের মধ্যে পার্থক্য থাকলে NoSuchFieldException বা IllegalStateException ত্রুটি হতে পারে।

উদাহরণ:

class Person {
    String name;
    int age;
}

String json = "{ \"first_name\": \"John\", \"age\": 30 }";  // 'first_name' does not match the field name in Java class
Gson gson = new Gson();
Person person = gson.fromJson(json, Person.class);  // Throws JsonSyntaxException or NoSuchFieldException

সমাধান: এই সমস্যার সমাধান হলো @SerializedName অ্যানোটেশন ব্যবহার করে Java ফিল্ড এবং JSON ফিল্ডের মধ্যে সঠিক ম্যাপিং নিশ্চিত করা।

সমাধান:

import com.google.gson.annotations.SerializedName;

class Person {
    @SerializedName("first_name")
    String name;
    
    int age;
}

String json = "{ \"first_name\": \"John\", \"age\": 30 }";
Gson gson = new Gson();
Person person = gson.fromJson(json, Person.class);  // Works correctly now
  • @SerializedName অ্যানোটেশন ব্যবহার করে JSON ফিল্ড নামের সাথে Java ফিল্ডের নাম ম্যাপ করুন।

3. Null Pointer Exception for Missing JSON Fields

সমস্যা: যদি JSON ডাটাতে কিছু ফিল্ড অনুপস্থিত থাকে, এবং Java ক্লাসে ঐ ফিল্ডের ডিফল্ট মান না থাকে, তবে NullPointerException ঘটতে পারে।

উদাহরণ:

class Person {
    String name;
    int age;
}

String json = "{ \"name\": \"John\" }";  // Missing 'age' field
Gson gson = new Gson();
Person person = gson.fromJson(json, Person.class);  // age will be initialized with default 0, no NPE

সমাধান: যদি কোনও ফিল্ডের জন্য ডিফল্ট মান সেট না করা থাকে, তবে আপনি @SerializedName অ্যানোটেশন ব্যবহার করে ডিফল্ট মান রাখতে পারেন, অথবা null চেক করতে পারেন।

সমাধান:

class Person {
    String name;
    
    @SerializedName("age")
    int age = 0;  // Default value to avoid NPE if missing in JSON
}

String json = "{ \"name\": \"John\" }";
Gson gson = new Gson();
Person person = gson.fromJson(json, Person.class);  // age will default to 0

4. Using Incorrect Data Types for JSON Fields

সমস্যা: যখন JSON ডাটার একটি ফিল্ডের জন্য ভিন্ন ধরনের ডাটা টাইপ থাকে এবং আপনি ভুল ডাটা টাইপ উল্লেখ করেন, তখন JsonSyntaxException বা ClassCastException হতে পারে।

উদাহরণ:

class Person {
    String name;
    int age;
}

String json = "{ \"name\": \"John\", \"age\": \"30\" }";  // 'age' is a String in JSON, but it's expected to be an int
Gson gson = new Gson();
Person person = gson.fromJson(json, Person.class);  // Throws JsonSyntaxException

সমাধান: JSON ডাটা এবং Java ক্লাসের ডাটা টাইপের মধ্যে মিল থাকতে হবে। আপনি @JsonAdapter অথবা TypeAdapter ব্যবহার করে কাস্টম টাইপ কনভার্সন তৈরি করতে পারেন।

সমাধান:

class Person {
    String name;
    
    @JsonAdapter(IntAdapter.class)
    int age;
}

class IntAdapter extends TypeAdapter<Integer> {
    @Override
    public Integer read(JsonReader in) throws IOException {
        return Integer.parseInt(in.nextString());  // Handle conversion from String to int
    }

    @Override
    public void write(JsonWriter out, Integer value) throws IOException {
        out.value(value.toString());
    }
}

String json = "{ \"name\": \"John\", \"age\": \"30\" }";
Gson gson = new Gson();
Person person = gson.fromJson(json, Person.class);  // Now works correctly

5. Circular References

সমস্যা: গন circular references (যেমন, এক অবজেক্টে অন্য একটি অবজেক্টের রেফারেন্স থাকে এবং বিপরীতেও একই) সঠিকভাবে ডেসিরিয়ালাইজ করতে পারে না, যার ফলে StackOverflowError হতে পারে।

উদাহরণ:

class Person {
    String name;
    Person friend;
}

Person john = new Person();
john.name = "John";
john.friend = john;

Gson gson = new Gson();
String json = gson.toJson(john);  // Throws StackOverflowError due to circular reference

সমাধান: এই ধরনের সমস্যার জন্য, @Expose এবং @SerializedName অ্যানোটেশন ব্যবহার করতে পারেন বা @JsonAdapter দিয়ে কাস্টম সলিউশন তৈরি করতে পারেন। এছাড়া, Gson এ ExclusionStrategy ব্যবহার করা যেতে পারে।

সমাধান:

import com.google.gson.*;
import com.google.gson.annotations.Expose;

class Person {
    @Expose
    String name;
    
    @Expose(serialize = false)
    Person friend;  // Do not serialize circular reference
}

Person john = new Person();
john.name = "John";
john.friend = john;

Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
String json = gson.toJson(john);  // No StackOverflowError

6. Handling Polymorphism

সমস্যা: যখন আপনার JSON ডাটাতে polymorphic অবজেক্ট থাকে এবং আপনি সাধারণ TypeAdapter ব্যবহার করেন না, তখন Gson সঠিকভাবে ডেসিরিয়ালাইজ করতে পারে না।

উদাহরণ:

class Animal {
    String name;
}

class Dog extends Animal {
    String breed;
}

String json = "{ \"name\": \"Buddy\", \"breed\": \"Labrador\" }";
Gson gson = new Gson();
Animal animal = gson.fromJson(json, Animal.class);  // Throws JsonParseException

সমাধান: Polymorphic অবজেক্টে Gson-এর TypeAdapter ব্যবহার করে কাস্টম ডেসিরিয়ালাইজেশন করতে হবে।

সমাধান:

class AnimalAdapter implements JsonSerializer<Animal>, JsonDeserializer<Animal> {
    @Override
    public JsonElement serialize(Animal src, Type typeOfSrc, JsonSerializationContext context) {
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("name", src.name);
        if (src instanceof Dog) {
            jsonObject.addProperty("type", "dog");
            jsonObject.addProperty("breed", ((Dog) src).breed);
        }
        return jsonObject;
    }

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

        if ("dog".equals(type)) {
            Dog dog = new Dog();
            dog.name = name;
            dog.breed = jsonObject.get("breed").getAsString();
            return dog;
        }
        return new Animal();  // Default case
    }
}

Gson gson = new GsonBuilder().registerTypeAdapter(Animal.class, new AnimalAdapter()).create();
Animal animal = gson.fromJson(json, Animal.class);  // Works fine now
  • JSON সিনট্যাক্স, টাইপ মিসম্যাচ, অনুপস্থিত ফিল্ড, এবং সাইক্লিক রেফারেন্স সম্পর্কিত সমস্যা Gson এর সাধারণ ভুল।
  • এই ভুলগুলি প্রতিরোধ করতে, @SerializedName, TypeAdapter, @Expose, ExclusionStrategy এবং Polymorphism Handling এর মতো কৌশল ব্যবহার করতে হবে।
  • Gson ব্যবহার করার সময় JSON ডাটা এবং Java অবজেক্টের মধ্যে সঠিক ম্যাপিং এবং ডাটা টাইপ নিশ্চিত করা খুবই গুরুত্বপূর্ণ।
Content added By
Promotion

Are you sure to start over?

Loading...