Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Caused by: java.lang.ClassCastException: libcore.reflect.ParameterizedTypeImpl cannot be cast to java.lang.Class when use Generic Type

I make a class extend AsyncTask to parse Json use Gson, because input can be many type of class I use generic type:

public class ApiResponse<T> extends AsyncTask...

I need know class type of T to pass Gson with:

(1)

Class<T> clazz =
    (Class<T>)((ParameterizedType)
        getClass().getGenericSuperclass())
    .getActualTypeArguments()[0];

new Gson().fromJson(reader, clazz);

However, Here T can be a class that have many type so that some time I pass class:

(2)

public class DataMessage<T> implements Serializable{...}

With these class have this format I received a Exception Caused by:

java.lang.ClassCastException: libcore.reflect.ParameterizedTypeImpl cannot be cast to java.lang.Class

at (1)

How do I do in this case ?

UPDATED:

public class DataMessage<T> implements Serializable{

    @SerializedName("pagination")
    private Pagination pagination;
    @SerializedName("meta")
    private Message meta;
    @SerializedName("data")
    private T data;

    public DataMessage(T data) {
        this.data = data;
    }

    public Pagination getPagination() {
        return pagination;
    }

    public void setPagination(Pagination pagination) {
        this.pagination = pagination;
    }

    public Message getMeta() {
        return meta;
    }

    public void setMeta(Message meta) {
        this.meta = meta;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public class Pagination {
        @SerializedName("next_max_tag_id")
        public String nextMaxTagId;
        @SerializedName("deprecation_warning")
        public String deprecationWarning;
        @SerializedName("next_max_id")
        public String nextMaxId;
        @SerializedName("next_min_id")
        public String nextMinId;
        @SerializedName("min_tag_id")
        public String minTagId;
        @SerializedName("next_url")
        public String nextUrl;
    }

    public class Message {
        @SerializedName("error_type")
        public String errorType;
        @SerializedName("code")
        public int code;
        @SerializedName("error_message")
        public String error_message;
    }
}
like image 723
mdtuyen Avatar asked Oct 30 '25 15:10

mdtuyen


1 Answers

When I need to parse JSON with specific class using Gson, I use GsonBuilder in this way

GsonBuilder gsonBuilder = new GsonBuilder();

gsonBuilder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
   public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
        try{
           return new Date( json.getAsJsonPrimitive().getAsLong()*1000L );
        }catch( Exception e ){
            return null;
        }

   }
});

Gson gson = gsonBuilder.create();

In your case I think that you can simply parse your JSON in this way:

DataMessage<YourClass> dataMessage = gson.fromJson(yourJsonObj, DataMessage.class);

Another thing is that you can obtain the exact Type in this way

Type collectionType = new TypeToken<DataMessage<T>>() {}.getType();

But probably you will get a bad Json when you deserialize this...

The best way is to use the class instead of T so Gson know exactly what has to serialie/deserialize

Type collectionType = new TypeToken<DataMessage<YourClass>>() {}.getType();

I found this for you Deserializing Generic Types with GSON

like image 199
Andrea Catania Avatar answered Nov 02 '25 07:11

Andrea Catania



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!