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

Fix JsonReader.hasNext() returning true at end of document #2061

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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
Expand Up @@ -47,4 +47,11 @@ public void testSkipValue_filledJsonObject() throws IOException {
in.skipValue();
assertEquals(JsonToken.END_DOCUMENT, in.peek());
}

public void testHasNext_endOfDocument() 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
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