Skip to content

Instantly share code, notes, and snippets.

@MSIH
Last active December 5, 2015 19:25
Show Gist options
  • Save MSIH/5b02bd8f5931b158c1c0 to your computer and use it in GitHub Desktop.
Save MSIH/5b02bd8f5931b158c1c0 to your computer and use it in GitHub Desktop.
Deserialize JSON Object into Java Class Based on JSON Key
class ClassDeserializer<T> implements JsonDeserializer<T> {
private Class<T> mClass;
private String mKey;
public ClassDeserializer(Class<T> targetClass, String key) {
mClass = targetClass;
mKey = key;
}
@Override
public T deserialize(JsonElement je, Type type, JsonDeserializationContext jdc)
throws JsonParseException {
JsonElement content = je.getAsJsonObject().get(mKey);
return new Gson().fromJson(content, mClass);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment