I know it's a frequently asked question, but after searching for two hours I'm asking here if there's a solution. I need to implement a third party interface that serializes and deserializes objects in an Android app. I'd like to use Gson to achieve this. The methods are the following:
@Override
public byte[] serialize(Object object) throws IOException {
return new Gson().toJson(object).getBytes();
}
@Override
public <T extends Job> T deserialize(byte[] bytes) throws IOException, ClassNotFoundException {
// problem here
}
I can't use something like this:
gson.fromJson(responseBody, new TypeToken<T>() {}.getType());
Because it returns an object of type LinkedTreeMap
.
Gson needs the Class of the object, but I've only this T
(I come from the .net world, where generics are more "developer friendly").
Is it possible to obtain the original class of T
in this scenario?