Log.d Log.d - 5 months ago 561x
Java Question

google gson LinkedTreeMap class cast to myclass

I knew this question has been asked before. Due to my novice skill in java and android. I can't resolve this issue for more than a week.

One of my friend and i developing an android project were there are a couple of things like this.

The most weird part of this things is, it's happening only from when i download and test it from Google play store. Not from local android studio installation or debug mode.

What could be the problem here, or this returning list which is totally wrong ?
My friend convincing that this code returns correctly but from play store installation it's always an error.

Please suggest me where should i keep digging?

public void promiseMethod(JSONObject object) {
if (object != null) {

if (object.has(DO_SERVICES)) {
vehicleDetails = new ArrayList < Object[] > (1);
List < String > vehicleNoList = new ArrayList < String > (1);
List < String > serviceList = new ArrayList < String > (1);
try {
Gson gson = new Gson();
JSONObject jsonObj = new JSONObject(object.get(DO_SERVICES)
servDto = gson.fromJson(jsonObj.toString(),

if (servDto.getServiceDto() instanceof List) {

List < DoServiceDto > doServiceList = servDto.getServiceDto();

Exception is

java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to com.gaurage.dto.DoServiceDto
at com.gaurage.user.User_Test_Main.promiseMethod(Unknown Source)


Serializing and Deserializing Generic Types

When you call toJson(obj), Gson calls obj.getClass() to get information on the fields to serialize. Similarly, you can typically pass MyClass.class object in the fromJson(json, MyClass.class) method. This works fine if the object is a non-generic type. However, if the object is of a generic type, then the Generic type information is lost because of Java Type Erasure. Here is an example illustrating the point:

class Foo<T> {  T value;}
Gson gson = new Gson();
Foo<Bar> foo = new Foo<Bar>();
gson.toJson(foo); // May not serialize foo.value correctly
gson.fromJson(json, foo.getClass()); // Fails to deserialize foo.value as Bar

The above code fails to interpret value as type Bar because Gson invokes list.getClass() to get its class information, but this method returns a raw class, Foo.class. This means that Gson has no way of knowing that this is an object of type Foo, and not just plain Foo.

You can solve this problem by specifying the correct parameterized type for your generic type. You can do this by using the TypeToken class.

Type fooType = new TypeToken<Foo<Bar>>() {}.getType();    
gson.toJson(foo, fooType);
gson.fromJson(json, fooType);

I have Parent class and it's child class some of them having List types in it. Like this parent class i have 30 files. I solved it like this.

Gson gson = new Gson();
                JSONObject jsonObj = new JSONObject(object.get(DO_SERVICES)
                Type type = new TypeToken<MyDto>() {
                servDto = gson.fromJson(jsonObj.toString(),

The most important thing is, I can't reproduce this error in local testing from Android studio. This problem pops up only, When i generate signed apk and publish app into PlayStore were the app stops, and the report says Cannot cast LinkedTreeMap to myclass.

It was hard for me to reproduce the same result in my local testing(includes Debug mode).