From 710a76c8b8835e36935c62024001ba6b91ceee01 Mon Sep 17 00:00:00 2001 From: Marcono1234 Date: Fri, 28 Jan 2022 00:59:50 +0100 Subject: [PATCH] Fix JsonReader.hasNext() returning true at end of document (#2061) --- .../java/com/google/gson/internal/bind/JsonTreeReader.java | 2 +- gson/src/main/java/com/google/gson/stream/JsonReader.java | 2 +- .../com/google/gson/internal/bind/JsonTreeReaderTest.java | 7 +++++++ .../test/java/com/google/gson/stream/JsonReaderTest.java | 7 +++++++ 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/gson/src/main/java/com/google/gson/internal/bind/JsonTreeReader.java b/gson/src/main/java/com/google/gson/internal/bind/JsonTreeReader.java index f8238bc2..a753402e 100644 --- a/gson/src/main/java/com/google/gson/internal/bind/JsonTreeReader.java +++ b/gson/src/main/java/com/google/gson/internal/bind/JsonTreeReader.java @@ -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 { diff --git a/gson/src/main/java/com/google/gson/stream/JsonReader.java b/gson/src/main/java/com/google/gson/stream/JsonReader.java index a8cb22aa..3eb38b70 100644 --- a/gson/src/main/java/com/google/gson/stream/JsonReader.java +++ b/gson/src/main/java/com/google/gson/stream/JsonReader.java @@ -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; } /** diff --git a/gson/src/test/java/com/google/gson/internal/bind/JsonTreeReaderTest.java b/gson/src/test/java/com/google/gson/internal/bind/JsonTreeReaderTest.java index f6a5bf33..1166381b 100644 --- a/gson/src/test/java/com/google/gson/internal/bind/JsonTreeReaderTest.java +++ b/gson/src/test/java/com/google/gson/internal/bind/JsonTreeReaderTest.java @@ -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()); + } } diff --git a/gson/src/test/java/com/google/gson/stream/JsonReaderTest.java b/gson/src/test/java/com/google/gson/stream/JsonReaderTest.java index 65cbd075..07e77aa8 100644 --- a/gson/src/test/java/com/google/gson/stream/JsonReaderTest.java +++ b/gson/src/test/java/com/google/gson/stream/JsonReaderTest.java @@ -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}"));