Last active
December 28, 2016 11:48
-
-
Save anirudhramanan/559fdc425f15683ee141fa26849205fc to your computer and use it in GitHub Desktop.
JsonObjectTypeAdapter
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
/** | |
* Type Adapter for {@link JsonObject} | |
*/ | |
public static final class JsonObjectTypeAdapter extends TypeAdapter<JsonObject> { | |
@Override | |
public void write(JsonWriter out, JsonObject value) throws IOException { | |
JsonElementTypeAdapter.writeJsonElement(out, value); | |
} | |
@Override | |
public JsonObject read(JsonReader in) throws IOException { | |
JsonElement jsonElement = JsonElementTypeAdapter.readJsonElement(in); | |
if (null != jsonElement && !jsonElement.isJsonObject()) { | |
throw new IOException("Could not parse it as a JsonObject"); | |
} | |
return null != jsonElement && jsonElement.isJsonObject() ? jsonElement.getAsJsonObject() : null; | |
} | |
} | |
/** | |
* Type Adapter for {@link JsonElement} | |
*/ | |
public static final class JsonElementTypeAdapter extends TypeAdapter<JsonElement> { | |
public static JsonElement readJsonElement(JsonReader in) throws IOException { | |
switch (in.peek()) { | |
case STRING: | |
return new JsonPrimitive(in.nextString()); | |
case NUMBER: | |
String number = in.nextString(); | |
return new JsonPrimitive(new LazilyParsedNumber(number)); | |
case BOOLEAN: | |
return new JsonPrimitive(in.nextBoolean()); | |
case NULL: | |
in.nextNull(); | |
return JsonNull.INSTANCE; | |
case BEGIN_ARRAY: | |
JsonArray array = new JsonArray(); | |
in.beginArray(); | |
while (in.hasNext()) { | |
array.add(readJsonElement(in)); | |
} | |
in.endArray(); | |
return array; | |
case BEGIN_OBJECT: | |
JsonObject object = new JsonObject(); | |
in.beginObject(); | |
while (in.hasNext()) { | |
object.add(in.nextName(), readJsonElement(in)); | |
} | |
in.endObject(); | |
return object; | |
case END_DOCUMENT: | |
case NAME: | |
case END_OBJECT: | |
case END_ARRAY: | |
default: | |
throw new IllegalArgumentException(); | |
} | |
} | |
public static void writeJsonElement(JsonWriter out, JsonElement value) throws IOException { | |
if (value == null || value.isJsonNull()) { | |
out.nullValue(); | |
} else if (value.isJsonPrimitive()) { | |
JsonPrimitive primitive = value.getAsJsonPrimitive(); | |
if (primitive.isNumber()) { | |
out.value(primitive.getAsNumber()); | |
} else if (primitive.isBoolean()) { | |
out.value(primitive.getAsBoolean()); | |
} else { | |
out.value(primitive.getAsString()); | |
} | |
} else if (value.isJsonArray()) { | |
out.beginArray(); | |
for (JsonElement e : value.getAsJsonArray()) { | |
writeJsonElement(out, e); | |
} | |
out.endArray(); | |
} else if (value.isJsonObject()) { | |
out.beginObject(); | |
for (Map.Entry<String, JsonElement> e : value.getAsJsonObject().entrySet()) { | |
out.name(e.getKey()); | |
writeJsonElement(out, e.getValue()); | |
} | |
out.endObject(); | |
} else { | |
throw new IllegalArgumentException("Couldn't write " + value.getClass()); | |
} | |
} | |
@Override | |
public JsonElement read(JsonReader in) throws IOException { | |
return JsonElementTypeAdapter.readJsonElement(in); | |
} | |
@Override | |
public void write(JsonWriter out, JsonElement value) throws IOException { | |
writeJsonElement(out, value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment