I have a class that deserializes ArrayList generalizations using this function, as described in the first answer of this thread: Java abstract class function generic Type
public <T> ArrayList<T> arrayType(String data){ return g.fromJson(data, TypeToken.get(new ArrayList<T>().getClass())); }
Eclipse asks me to use TypeToken, resulting in (the sinde function fromJson needs a type, not a Token type)
public <T> ArrayList<T> arrayType(String data){ return g.fromJson(data, (Type) TypeToken.get(new ArrayList<T>().getClass())); }
As a result, I get this error:
java.lang.ClassCastException: com.google.gson.reflect.TypeToken cannot be cast to java.lang.reflect.Type
In the gson user manual, they tell you that this is the correct way to call a function
Type collectionType = new TypeToken<Collection<Integer>>(){}.getType(); Collection<Integer> ints2 = gson.fromJson(json, collectionType);
and I donβt see what I am doing wrong (if this is the correct answer, why do I get this error when throwing?)
dinigo
source share