0

I have a generic class. It has a method which deserializes an object and returns it:

public class MyClass<T> {
 T storage;
 private T get() throws InterruptedException {
    T clone=gson.fromJson(storage,myclass); //not working
    return clone;
  }
}

My problem is that fromJson requieres a Class as second parameter. How can I get it from my Type T? Is there a generic implementation of fromJson?

EDIT: is there a generic implementation of Gson how to avoid this constructur (mentioned in the question) for getting the class?

user3579222
  • 1,103
  • 11
  • 28
  • Possible duplicate of [Get generic type of class at runtime](https://stackoverflow.com/questions/3403909/get-generic-type-of-class-at-runtime) – fjc Jul 08 '19 at 10:06
  • Mmmh something's off in your `T clone=gson.fromJson(storage,myclass);` line: the first parameter of `fromJson` should be the JSON not an instance of `T`, for the second parameter you could simply use `storage.class` like this: `T clone=gson.fromJson("{}", storage.class);` – Carlo Moretti Jul 24 '19 at 09:25

0 Answers0