Skip to content

Instantly share code, notes, and snippets.

@anirudhramanan
Last active December 28, 2016 11:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anirudhramanan/559fdc425f15683ee141fa26849205fc to your computer and use it in GitHub Desktop.
Save anirudhramanan/559fdc425f15683ee141fa26849205fc to your computer and use it in GitHub Desktop.
JsonObjectTypeAdapter
/**
* 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