0

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, i.e. 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?

user3038457
  • 185
  • 2
  • 11
  • 1
    This `new TypeToken>(){}.getType()` doesn't do what you expect. However, the error you are showing us is unrelated to the code you posted, afaik. Please post a [mcve]. – Sotirios Delimanolis Aug 27 '16 at 00:15
  • The stack trace from the error I received links back to the lines corresponding to exactly what I posted. Can you explain why the error is unrelated to my code? I do not mean to be unclear – user3038457 Aug 27 '16 at 00:21
  • 1
    I can't reproduce it. I call your `parseJson` with some corresponding JSON and it does not fail with the exception you suggested. – Sotirios Delimanolis Aug 27 '16 at 00:24
  • You are right. I wish I could paste my original code that is causing this error but I can't as it is private for work. And I am attempting to replicate everything relevant to my situation as specifically as possible as well, with no such luck in recreating the error. This is only further confusing me...I will attempt to update the question with further information if I am able to attain it. My apologies. – user3038457 Aug 27 '16 at 00:55
  • @SotiriosDelimanolis After some digging I found the source of the topical error. 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. However, now I encountered a different error which I will address in a separate question – user3038457 Aug 29 '16 at 22:15
  • The fact that they were nested classes is something you could have posted. Please do so now. – Sotirios Delimanolis Aug 29 '16 at 22:18
  • Edited. And posted a [more coherent question](http://stackoverflow.com/questions/39216233/deserializing-to-a-templated-class-with-different-possible-parameterized-types) with a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) – user3038457 Aug 29 '16 at 22:50

1 Answers1

0

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

user3038457
  • 185
  • 2
  • 11