Implemented suggestions from the code review of r436: throwing NoSuchElementException in case the stream hits EOF.
This commit is contained in:
parent
3b1056c097
commit
c64b79c0f9
@ -19,6 +19,7 @@ import java.io.EOFException;
|
|||||||
import java.io.Reader;
|
import java.io.Reader;
|
||||||
import java.io.StringReader;
|
import java.io.StringReader;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
import java.util.NoSuchElementException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A streaming parser that allows reading of multiple {@link JsonElement}s from the specified reader
|
* 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<JsonElement> {
|
public final class JsonStreamParser implements Iterator<JsonElement> {
|
||||||
|
|
||||||
private final JsonParserJavacc parser;
|
private final JsonParserJavacc parser;
|
||||||
private boolean eof;
|
|
||||||
private JsonElement nextElement;
|
private JsonElement nextElement;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -48,7 +48,6 @@ public final class JsonStreamParser implements Iterator<JsonElement> {
|
|||||||
*/
|
*/
|
||||||
public JsonStreamParser(Reader reader) {
|
public JsonStreamParser(Reader reader) {
|
||||||
parser = new JsonParserJavacc(reader);
|
parser = new JsonParserJavacc(reader);
|
||||||
eof = false;
|
|
||||||
nextElement = null;
|
nextElement = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -60,17 +59,13 @@ public final class JsonStreamParser implements Iterator<JsonElement> {
|
|||||||
* @since 1.4
|
* @since 1.4
|
||||||
*/
|
*/
|
||||||
public JsonElement next() throws JsonParseException {
|
public JsonElement next() throws JsonParseException {
|
||||||
if (eof) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
if (nextElement != null) {
|
if (nextElement != null) {
|
||||||
JsonElement returnValue = nextElement;
|
JsonElement returnValue = nextElement;
|
||||||
nextElement = null;
|
nextElement = null;
|
||||||
return returnValue;
|
return returnValue;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
JsonElement element = parser.parse();
|
return parser.parse();
|
||||||
return element;
|
|
||||||
} catch (TokenMgrError e) {
|
} catch (TokenMgrError e) {
|
||||||
throw new JsonParseException("Failed parsing JSON source to Json", e);
|
throw new JsonParseException("Failed parsing JSON source to Json", e);
|
||||||
} catch (ParseException e) {
|
} catch (ParseException e) {
|
||||||
@ -81,8 +76,7 @@ public final class JsonStreamParser implements Iterator<JsonElement> {
|
|||||||
throw new JsonParseException("Failed parsing JSON source to Json", e);
|
throw new JsonParseException("Failed parsing JSON source to Json", e);
|
||||||
} catch (JsonParseException e) {
|
} catch (JsonParseException e) {
|
||||||
if (e.getCause() instanceof EOFException) {
|
if (e.getCause() instanceof EOFException) {
|
||||||
eof = true;
|
throw new NoSuchElementException();
|
||||||
return null;
|
|
||||||
} else {
|
} else {
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
@ -90,8 +84,13 @@ public final class JsonStreamParser implements Iterator<JsonElement> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean hasNext() {
|
public boolean hasNext() {
|
||||||
nextElement = next();
|
try {
|
||||||
return nextElement != null;
|
nextElement = next();
|
||||||
|
return true;
|
||||||
|
} catch (NoSuchElementException e) {
|
||||||
|
nextElement = null;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void remove() {
|
public void remove() {
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
package com.google.gson;
|
package com.google.gson;
|
||||||
|
|
||||||
import java.util.Iterator;
|
|
||||||
|
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.NoSuchElementException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unit tests for {@link JsonStreamParser}
|
* Unit tests for {@link JsonStreamParser}
|
||||||
*
|
*
|
||||||
@ -27,4 +28,15 @@ public class JsonStreamParserTest extends TestCase {
|
|||||||
assertEquals("two", parser.next().getAsString());
|
assertEquals("two", parser.next().getAsString());
|
||||||
assertFalse(parser.hasNext());
|
assertFalse(parser.hasNext());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testCallingNextBeyondAvailableInput() {
|
||||||
|
Iterator<JsonElement> parser = new JsonStreamParser("'one' 'two'");
|
||||||
|
parser.next();
|
||||||
|
parser.next();
|
||||||
|
try {
|
||||||
|
parser.next();
|
||||||
|
fail("Parser should not go beyond available input");
|
||||||
|
} catch (NoSuchElementException expected) {
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,6 @@ package com.google.gson.functional;
|
|||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.GsonBuilder;
|
import com.google.gson.GsonBuilder;
|
||||||
import com.google.gson.JsonElement;
|
|
||||||
import com.google.gson.JsonStreamParser;
|
import com.google.gson.JsonStreamParser;
|
||||||
import com.google.gson.common.TestTypes.BagOfPrimitives;
|
import com.google.gson.common.TestTypes.BagOfPrimitives;
|
||||||
|
|
||||||
@ -112,7 +111,6 @@ public class ReadersWritersTest extends TestCase {
|
|||||||
assertEquals("one", actualOne.stringValue);
|
assertEquals("one", actualOne.stringValue);
|
||||||
BagOfPrimitives actualTwo = gson.fromJson(parser.next(), BagOfPrimitives.class);
|
BagOfPrimitives actualTwo = gson.fromJson(parser.next(), BagOfPrimitives.class);
|
||||||
assertEquals("two", actualTwo.stringValue);
|
assertEquals("two", actualTwo.stringValue);
|
||||||
JsonElement jsonElement = parser.next();
|
assertFalse(parser.hasNext());
|
||||||
assertNull(jsonElement);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user