Last active
December 28, 2016 11:48
-
-
Save anirudhramanan/25a02673c2338b51d6e49186bd54f3a6 to your computer and use it in GitHub Desktop.
JsonArrayTypeAdapter
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 JsonArray} | |
*/ | |
public static final class JsonArrayTypeAdapter extends TypeAdapter<JsonArray> { | |
@Override | |
public void write(JsonWriter out, JsonArray value) throws IOException { | |
JsonElementTypeAdapter.writeJsonElement(out, value); | |
} | |
@Override | |
public JsonArray read(JsonReader in) throws IOException { | |
JsonElement jsonElement = JsonElementTypeAdapter.readJsonElement(in); | |
if (null != jsonElement && !jsonElement.isJsonArray()) { | |
throw new IOException("Could not parse it as a JsonArray"); | |
} | |
return null != jsonElement && jsonElement.isJsonArray() ? jsonElement.getAsJsonArray() : 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