diff --git a/gson/src/main/java/com/google/gson/JsonStreamParser.java b/gson/src/main/java/com/google/gson/JsonStreamParser.java index bb7c2374..e7a02174 100644 --- a/gson/src/main/java/com/google/gson/JsonStreamParser.java +++ b/gson/src/main/java/com/google/gson/JsonStreamParser.java @@ -19,6 +19,7 @@ import java.io.EOFException; import java.io.Reader; import java.io.StringReader; import java.util.Iterator; +import java.util.NoSuchElementException; /** * A streaming parser that allows reading of multiple {@link JsonElement}s from the specified reader @@ -31,7 +32,6 @@ import java.util.Iterator; public final class JsonStreamParser implements Iterator { private final JsonParserJavacc parser; - private boolean eof; private JsonElement nextElement; /** @@ -48,7 +48,6 @@ public final class JsonStreamParser implements Iterator { */ public JsonStreamParser(Reader reader) { parser = new JsonParserJavacc(reader); - eof = false; nextElement = null; } @@ -60,17 +59,13 @@ public final class JsonStreamParser implements Iterator { * @since 1.4 */ public JsonElement next() throws JsonParseException { - if (eof) { - return null; - } if (nextElement != null) { JsonElement returnValue = nextElement; nextElement = null; return returnValue; } try { - JsonElement element = parser.parse(); - return element; + return parser.parse(); } catch (TokenMgrError e) { throw new JsonParseException("Failed parsing JSON source to Json", e); } catch (ParseException e) { @@ -81,8 +76,7 @@ public final class JsonStreamParser implements Iterator { throw new JsonParseException("Failed parsing JSON source to Json", e); } catch (JsonParseException e) { if (e.getCause() instanceof EOFException) { - eof = true; - return null; + throw new NoSuchElementException(); } else { throw e; } @@ -90,8 +84,13 @@ public final class JsonStreamParser implements Iterator { } public boolean hasNext() { - nextElement = next(); - return nextElement != null; + try { + nextElement = next(); + return true; + } catch (NoSuchElementException e) { + nextElement = null; + return false; + } } public void remove() { diff --git a/gson/src/test/java/com/google/gson/JsonStreamParserTest.java b/gson/src/test/java/com/google/gson/JsonStreamParserTest.java index 82da09f5..a1051024 100644 --- a/gson/src/test/java/com/google/gson/JsonStreamParserTest.java +++ b/gson/src/test/java/com/google/gson/JsonStreamParserTest.java @@ -1,9 +1,10 @@ package com.google.gson; -import java.util.Iterator; - import junit.framework.TestCase; +import java.util.Iterator; +import java.util.NoSuchElementException; + /** * Unit tests for {@link JsonStreamParser} * @@ -27,4 +28,15 @@ public class JsonStreamParserTest extends TestCase { assertEquals("two", parser.next().getAsString()); assertFalse(parser.hasNext()); } + + public void testCallingNextBeyondAvailableInput() { + Iterator parser = new JsonStreamParser("'one' 'two'"); + parser.next(); + parser.next(); + try { + parser.next(); + fail("Parser should not go beyond available input"); + } catch (NoSuchElementException expected) { + } + } } diff --git a/gson/src/test/java/com/google/gson/functional/ReadersWritersTest.java b/gson/src/test/java/com/google/gson/functional/ReadersWritersTest.java index 98e8fd14..95b90ef6 100644 --- a/gson/src/test/java/com/google/gson/functional/ReadersWritersTest.java +++ b/gson/src/test/java/com/google/gson/functional/ReadersWritersTest.java @@ -17,7 +17,6 @@ package com.google.gson.functional; import com.google.gson.Gson; import com.google.gson.GsonBuilder; -import com.google.gson.JsonElement; import com.google.gson.JsonStreamParser; import com.google.gson.common.TestTypes.BagOfPrimitives; @@ -112,7 +111,6 @@ public class ReadersWritersTest extends TestCase { assertEquals("one", actualOne.stringValue); BagOfPrimitives actualTwo = gson.fromJson(parser.next(), BagOfPrimitives.class); assertEquals("two", actualTwo.stringValue); - JsonElement jsonElement = parser.next(); - assertNull(jsonElement); + assertFalse(parser.hasNext()); } }