Improve the speed of the JSON_ELEMENT TypeAdapter when the object graph has already been turned into a JsonElement

This commit is contained in:
Simon Guerout 2021-08-20 16:02:18 -04:00 committed by Éamonn McManus
parent 1023f0fe34
commit 62a9702385
2 changed files with 13 additions and 0 deletions

View File

@ -249,6 +249,15 @@ public final class JsonTreeReader extends JsonReader {
return result;
}
JsonElement nextJsonElement() throws IOException {
if (peek() == JsonToken.NAME) {
throw new IllegalStateException("Can't turn a name into a JsonElement");
}
final JsonElement element = (JsonElement) peekStack();
skipValue();
return element;
}
@Override public void close() throws IOException {
stack = new Object[] { SENTINEL_CLOSED };
stackSize = 1;

View File

@ -677,6 +677,10 @@ public final class TypeAdapters {
public static final TypeAdapter<JsonElement> JSON_ELEMENT = new TypeAdapter<JsonElement>() {
@Override public JsonElement read(JsonReader in) throws IOException {
if (in instanceof JsonTreeReader) {
return ((JsonTreeReader) in).nextJsonElement();
}
switch (in.peek()) {
case STRING:
return new JsonPrimitive(in.nextString());