Skip to content

Instantly share code, notes, and snippets.

@NLMartian
Last active April 30, 2016 15:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NLMartian/90172e52f3e5e2ed1648522c9a60fb7f to your computer and use it in GitHub Desktop.
Save NLMartian/90172e52f3e5e2ed1648522c9a60fb7f to your computer and use it in GitHub Desktop.
Google Gson - deserialize list<class> object
public static List<T> fromJsonList(String json, Class<T> klass) {
Gson gson = new Gson();
return gson.fromJson(json, new ListOfSomething<T>(klass));
}
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