I'm building an Android app which uses Gson to serialize and deserialize objects from an API. The objects are POJOs, and I have an APIProvider class which is extended by individual Providers for each type of object (e.g. ProfileProvider extends APIProvider). I've created the APIProvider generic class in order to abstract caching.
The problem I'm having is with generics in the generic class APIProvider.
If I use the following line:
List<T> objects = gson.fromJson(cachedObjects, new TypeToken<List<Profile>>(){}.getType());
then everything works correctly. As soon as I try to abstract Profile and do the following:
List<T> objects = gson.fromJson(cachedObjects, new TypeToken<List<T>>(){}.getType());
I get the following error:
java.lang.ClassCastException: com.google.gson.internal.StringMap cannot be cast to org.buffer.android.core.Profile
Any help would be awesome.
>(){}.getType()` to the method as `mType` and then used `List objects = gson.fromJson(cachedObjects, mType);`. Appreciate the speedy help.
– joelg Sep 05 '12 at 06:53