Fix hasNext to return false at document end.

This commit is contained in:
Eric Cochran
2018-06-22 15:18:22 -07:00
parent 03ada87e90
commit 627e62f507
3 changed files with 8 additions and 3 deletions

View File

@@ -162,7 +162,7 @@ final class JsonUtf8Reader extends JsonReader {
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;
}
@Override public Token peek() throws IOException {

View File

@@ -101,8 +101,7 @@ final class JsonValueReader extends JsonReader {
}
@Override public boolean hasNext() throws IOException {
// TODO(jwilson): this is consistent with BufferedSourceJsonReader but it doesn't make sense.
if (stackSize == 0) return true;
if (stackSize == 0) return false;
Object peeked = stack[stackSize - 1];
return !(peeked instanceof Iterator) || ((Iterator) peeked).hasNext();

View File

@@ -979,4 +979,10 @@ public final class JsonReaderTest {
}
assertThat(reader.nextInt()).isEqualTo(1);
}
@Test public void emptyDocumentHasNextReturnsFalse() throws IOException {
JsonReader reader = newReader("1");
reader.readJsonValue();
assertThat(reader.hasNext()).isFalse();
}
}