I have a class with a nested subclass
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;}
}
}
Currently I would like to be able to have a function that takes as a parameter the held class, and returns an appropriate instance of GenericClassHolder, ie something like my current code:
GenericClassHolder<ExampleClassA> genericA = parseJson(jsonString, ExampleClassA.class);
with
<T> GenericClassHolder<T> parseJson(final String jsonString, Class<T> variableClass) throws JsonSyntaxException {
return GSON.fromJson(jsonString, new TypeToken<GenericClassHolder<T>>(){}.getType());
}
But this does not work in my current implementation, with the error
Unable to invoke no-args constructor for
GenericClassHolder<T>
. Register an InstanceCreator with Gson for this type may fix this problem.
How can I succinctly achieve my aforementioned goal of having a function that takes as input the specification for a class that may change and parses to this object accordingly? If it is not possible to achieve an elegant solution through a function, how can I achieve this goal without one?
I deployed this code in a Google Appengine instance, and due to some security reasons, I could not deserialize the way I was deserializing and replicate the same way on my local machine. The solution was to make the deserialized classes static; they were nested inside of an outer encapsulating class as well
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.