Created
April 28, 2017 07:44
-
-
Save piruin/cd54a3852933ed90710a8216e2c58d1b to your computer and use it in GitHub Desktop.
GSON's JsonAdapter for handle `{ @nil:true }` in tranfromed JSON from XML
This file contains hidden or 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
import com.google.gson.JsonDeserializationContext; | |
import com.google.gson.JsonDeserializer; | |
import com.google.gson.JsonElement; | |
import com.google.gson.JsonObject; | |
import com.google.gson.JsonParseException; | |
import java.lang.reflect.Type; | |
class Nillable<T> implements JsonDeserializer<T> { | |
@Override | |
public T deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) | |
throws JsonParseException | |
{ | |
if (json.isJsonObject()) { | |
JsonObject object = json.getAsJsonObject(); | |
if (object.has("@nil")) { | |
return null; | |
} | |
} | |
return context.deserialize(json, typeOfT); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment