I have a class with nested subclasses
public class OuterClass {
class GenericClassHolder<R> {
@SerializedName("results")
List<R> results;
public GenericClassHolder() {}
public List<R> getResults() {
return results;
}
}
class ExampleClassA {
@SerializedName("A")
int a;
public ExampleClassA() {}
public int getA() {return a;}
}
class ExampleClassB {
@SerializedName("B")
String b;
public ExampleClassA() {}
public String getB() {return b;}
}
}
And I would like to deserialize to different versions of GenericClassHolder in different circumstances. Currently I have
public <T> OuterClass.GenericClassHolder<T> parseJson(final String responseStr, final Class<T> responseClass) throws JsonSyntaxException {
return Gsons.getInstance().fromJson(responseStr, new TypeToken<OuterClass.GenericClassHolder<T>>() {
}.getType());
}
String jsonString = "{results:[{A:4}]}";
OuterClass.GenericClassHolder<OuterClass.ExampleClassA> ex = parseApiResponse(jsonString, OuterClass.ExampleClassA.class);
which seems to not break anything but when I get to the following line
System.out.println("I read the string and parsed it! A: " + ex.getResults().get(0).getA());
I get the error
Exception in thread "main" java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to OuterClass$ExampleClassA
How can I achieve the goal of being able to parse to a templated class that contains a list of the parameterized type?
-I am using Java 7.