I am trying to get right a method called fromJSON using the GSON library
public <T> T fromJSON( String object, Class<T> classObject )
{
return gson.fromJson( object, classObject );
}
The fact is that I have some classes that one member could be defined using Java generics. For example I can create a new ValidationResult<User>(), ValidationResult<Image>() or ListResult<User>(), ListResult<Image>()
, etc... so the problem I'm facing is that I need to tell GSON which T it should use. Here is where I am running in an issue, cause my current code is converting the JSON object into a ValidationResult
or ListResult
, but the generic object is not treated as User
, Image
, etc. It is just converted to a LinkedTreeMap
for example.
I tried some answers I have found in Stackoverflow, but I still am not able to get it right.
Using a generic type with Gson
Google Gson - deserialize list<class> object? (generic type)
Deserializing Generic Types with GSON
Can someone put myself in the right path? If possible share some code that I can use as guide.