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

This commit is contained in:
Marcono1234 2022-01-28 00:59:50 +01:00 committed by GitHub
parent be0a1f4ff7
commit 710a76c8b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 2 deletions

View File

@ -101,7 +101,7 @@ public final class JsonTreeReader extends JsonReader {
@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 {

View File

@ -413,7 +413,7 @@ public class JsonReader implements Closeable {
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;
}
/**

View File

@ -47,4 +47,11 @@ public class JsonTreeReaderTest extends TestCase {
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());
}
}

View File

@ -72,6 +72,13 @@ public final class JsonReaderTest extends TestCase {
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}"));