From 61f4cb1789185f1e1028464f0a4f11b64b210236 Mon Sep 17 00:00:00 2001 From: Oskar Date: Fri, 25 Feb 2022 14:22:31 +0100 Subject: [PATCH 1/2] Refactoring in JsonReader --- .../com/google/gson/stream/JsonReader.java | 79 ++++++------------- 1 file changed, 24 insertions(+), 55 deletions(-) diff --git a/gson/src/main/java/com/google/gson/stream/JsonReader.java b/gson/src/main/java/com/google/gson/stream/JsonReader.java index 3eb38b705b..b927458656 100644 --- a/gson/src/main/java/com/google/gson/stream/JsonReader.java +++ b/gson/src/main/java/com/google/gson/stream/JsonReader.java @@ -338,10 +338,7 @@ public final boolean isLenient() { * beginning of a new array. */ public void beginArray() throws IOException { - int p = peeked; - if (p == PEEKED_NONE) { - p = doPeek(); - } + int p = peeked(); if (p == PEEKED_BEGIN_ARRAY) { push(JsonScope.EMPTY_ARRAY); pathIndices[stackSize - 1] = 0; @@ -356,10 +353,7 @@ public void beginArray() throws IOException { * end of the current array. */ public void endArray() throws IOException { - int p = peeked; - if (p == PEEKED_NONE) { - p = doPeek(); - } + int p = peeked(); if (p == PEEKED_END_ARRAY) { stackSize--; pathIndices[stackSize - 1]++; @@ -374,10 +368,7 @@ public void endArray() throws IOException { * beginning of a new object. */ public void beginObject() throws IOException { - int p = peeked; - if (p == PEEKED_NONE) { - p = doPeek(); - } + int p = peeked(); if (p == PEEKED_BEGIN_OBJECT) { push(JsonScope.EMPTY_OBJECT); peeked = PEEKED_NONE; @@ -391,10 +382,7 @@ public void beginObject() throws IOException { * end of the current object. */ public void endObject() throws IOException { - int p = peeked; - if (p == PEEKED_NONE) { - p = doPeek(); - } + int p = peeked(); if (p == PEEKED_END_OBJECT) { stackSize--; pathNames[stackSize] = null; // Free the last path name so that it can be garbage collected! @@ -406,13 +394,21 @@ public void endObject() throws IOException { } /** - * Returns true if the current array or object has another element. + * Returns peeked if not PEEKED_NONE, if PEEKED_NONE then doPeek() is returned instead */ - public boolean hasNext() throws IOException { + private int peeked() { int p = peeked; if (p == PEEKED_NONE) { - p = doPeek(); + p = doPeek(); } + return p; + } + + /** + * Returns true if the current array or object has another element. + */ + public boolean hasNext() throws IOException { + int p = peeked(); return p != PEEKED_END_OBJECT && p != PEEKED_END_ARRAY && p != PEEKED_EOF; } @@ -420,10 +416,7 @@ public boolean hasNext() throws IOException { * Returns the type of the next token without consuming it. */ public JsonToken peek() throws IOException { - int p = peeked; - if (p == PEEKED_NONE) { - p = doPeek(); - } + int p = peeked(); switch (p) { case PEEKED_BEGIN_OBJECT: @@ -774,10 +767,7 @@ private boolean isLiteral(char c) throws IOException { * name. */ public String nextName() throws IOException { - int p = peeked; - if (p == PEEKED_NONE) { - p = doPeek(); - } + int p = peeked(); String result; if (p == PEEKED_UNQUOTED_NAME) { result = nextUnquotedValue(); @@ -802,10 +792,7 @@ public String nextName() throws IOException { * this reader is closed. */ public String nextString() throws IOException { - int p = peeked; - if (p == PEEKED_NONE) { - p = doPeek(); - } + int p = peeked(); String result; if (p == PEEKED_UNQUOTED) { result = nextUnquotedValue(); @@ -837,10 +824,7 @@ public String nextString() throws IOException { * this reader is closed. */ public boolean nextBoolean() throws IOException { - int p = peeked; - if (p == PEEKED_NONE) { - p = doPeek(); - } + int p = peeked(); if (p == PEEKED_TRUE) { peeked = PEEKED_NONE; pathIndices[stackSize - 1]++; @@ -861,10 +845,7 @@ public boolean nextBoolean() throws IOException { * reader is closed. */ public void nextNull() throws IOException { - int p = peeked; - if (p == PEEKED_NONE) { - p = doPeek(); - } + int p = peeked(); if (p == PEEKED_NULL) { peeked = PEEKED_NONE; pathIndices[stackSize - 1]++; @@ -883,10 +864,7 @@ public void nextNull() throws IOException { * as a double, or is non-finite. */ public double nextDouble() throws IOException { - int p = peeked; - if (p == PEEKED_NONE) { - p = doPeek(); - } + int p = peeked(); if (p == PEEKED_LONG) { peeked = PEEKED_NONE; @@ -928,10 +906,7 @@ public double nextDouble() throws IOException { * as a number, or exactly represented as a long. */ public long nextLong() throws IOException { - int p = peeked; - if (p == PEEKED_NONE) { - p = doPeek(); - } + int p = peeked(); if (p == PEEKED_LONG) { peeked = PEEKED_NONE; @@ -1160,10 +1135,7 @@ private void skipUnquotedValue() throws IOException { * as a number, or exactly represented as an int. */ public int nextInt() throws IOException { - int p = peeked; - if (p == PEEKED_NONE) { - p = doPeek(); - } + int p = peeked(); int result; if (p == PEEKED_LONG) { @@ -1227,10 +1199,7 @@ public void close() throws IOException { public void skipValue() throws IOException { int count = 0; do { - int p = peeked; - if (p == PEEKED_NONE) { - p = doPeek(); - } + int p = peeked(); if (p == PEEKED_BEGIN_ARRAY) { push(JsonScope.EMPTY_ARRAY); From d4b82f8d65e92de77d0509763acd8fda8faaee5c Mon Sep 17 00:00:00 2001 From: Oskar Edvardsson Date: Sat, 26 Feb 2022 10:11:45 +0100 Subject: [PATCH 2/2] Update gson/src/main/java/com/google/gson/stream/JsonReader.java Co-authored-by: Marcono1234 --- gson/src/main/java/com/google/gson/stream/JsonReader.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gson/src/main/java/com/google/gson/stream/JsonReader.java b/gson/src/main/java/com/google/gson/stream/JsonReader.java index b927458656..74d48747f8 100644 --- a/gson/src/main/java/com/google/gson/stream/JsonReader.java +++ b/gson/src/main/java/com/google/gson/stream/JsonReader.java @@ -399,7 +399,7 @@ public void endObject() throws IOException { private int peeked() { int p = peeked; if (p == PEEKED_NONE) { - p = doPeek(); + p = doPeek(); } return p; }