Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring and reducing code duplication in JsonReader #2083

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 23 additions & 54 deletions gson/src/main/java/com/google/gson/stream/JsonReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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]++;
Expand All @@ -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;
Expand All @@ -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!
Expand All @@ -406,24 +394,29 @@ 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();
}
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;
}

/**
* 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:
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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]++;
Expand All @@ -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]++;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down