Skip to content

Commit

Permalink
Fix JsonReader.hasNext() returning true at end of document
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcono1234 committed Jan 23, 2022
1 parent 8e01b54 commit 60db6d8
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public JsonTreeReader(JsonElement element) {

@Override public boolean hasNext() throws IOException {
JsonToken token = peek();
return token != JsonToken.END_OBJECT && token != JsonToken.END_ARRAY;
return token != JsonToken.END_OBJECT && token != JsonToken.END_ARRAY && token != JsonToken.END_DOCUMENT;
}

@Override public JsonToken peek() throws IOException {
Expand Down
2 changes: 1 addition & 1 deletion gson/src/main/java/com/google/gson/stream/JsonReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ public boolean hasNext() throws IOException {
if (p == PEEKED_NONE) {
p = doPeek();
}
return p != PEEKED_END_OBJECT && p != PEEKED_END_ARRAY;
return p != PEEKED_END_OBJECT && p != PEEKED_END_ARRAY && p != PEEKED_EOF;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,11 @@ public void testSkipValue_filledJsonObject() throws IOException {
in.skipValue();
assertEquals(JsonToken.END_DOCUMENT, in.peek());
}

public void testHasNextEndOfDocument() throws IOException {
JsonTreeReader reader = new JsonTreeReader(new JsonObject());
reader.beginObject();
reader.endObject();
assertFalse(reader.hasNext());
}
}
7 changes: 7 additions & 0 deletions gson/src/test/java/com/google/gson/stream/JsonReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ public void testReadEmptyObject() throws IOException {
assertEquals(JsonToken.END_DOCUMENT, reader.peek());
}

public void testHasNextEndOfDocument() throws IOException {
JsonReader reader = new JsonReader(reader("{}"));
reader.beginObject();
reader.endObject();
assertFalse(reader.hasNext());
}

public void testSkipArray() throws IOException {
JsonReader reader = new JsonReader(reader(
"{\"a\": [\"one\", \"two\", \"three\"], \"b\": 123}"));
Expand Down

0 comments on commit 60db6d8

Please sign in to comment.