Last active
December 5, 2015 19:25
-
-
Save MSIH/5b02bd8f5931b158c1c0 to your computer and use it in GitHub Desktop.
Deserialize JSON Object into Java Class Based on JSON Key
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
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