Last active
April 30, 2016 15:15
-
-
Save NLMartian/90172e52f3e5e2ed1648522c9a60fb7f to your computer and use it in GitHub Desktop.
Google Gson - deserialize list<class> object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static List<T> fromJsonList(String json, Class<T> klass) { | |
Gson gson = new Gson(); | |
return gson.fromJson(json, new ListOfSomething<T>(klass)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ListOfJson<T> implements ParameterizedType { | |
private Class<?> wrapped; | |
public ListOfJson(Class<T> wrapper) { | |
this.wrapped = wrapper; | |
} | |
@Override public Type[] getActualTypeArguments() { | |
return new Type[] { wrapped }; | |
} | |
@Override public Type getRawType() { | |
return List.class; | |
} | |
@Override public Type getOwnerType() { | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment