Made JsonParser implement Iterator

This commit is contained in:
Inderjeet Singh 2009-08-12 21:01:42 +00:00
parent 41a1f8b89e
commit f4f596ec3d
3 changed files with 47 additions and 11 deletions

View File

@ -18,18 +18,21 @@ package com.google.gson;
import java.io.EOFException; import java.io.EOFException;
import java.io.Reader; import java.io.Reader;
import java.io.StringReader; import java.io.StringReader;
import java.util.Iterator;
/** /**
* A parser that allows reading of multiple {@link JsonElement}s from the specified reader * A parser that allows reading of multiple {@link JsonElement}s from the specified reader
* asynchronously. * asynchronously. This class is not thread-safe.
* *
* @author Inderjeet Singh * @author Inderjeet Singh
* @author Joel Leitch * @author Joel Leitch
* @since 1.4 * @since 1.4
*/ */
public final class JsonParserAsync { public final class JsonParserAsync implements Iterator<JsonElement> {
private final JsonParserJavacc parser; private final JsonParserJavacc parser;
private boolean eof;
private JsonElement nextElement;
/** /**
* @param json The string containing JSON elements concatenated to each other. * @param json The string containing JSON elements concatenated to each other.
@ -45,6 +48,8 @@ public final class JsonParserAsync {
*/ */
public JsonParserAsync(Reader reader) { public JsonParserAsync(Reader reader) {
parser = new JsonParserJavacc(reader); parser = new JsonParserJavacc(reader);
eof = false;
nextElement = null;
} }
/** /**
@ -54,7 +59,15 @@ public final class JsonParserAsync {
* @throws JsonParseException if the incoming stream is malformed JSON. * @throws JsonParseException if the incoming stream is malformed JSON.
* @since 1.4 * @since 1.4
*/ */
public JsonElement nextElement() throws JsonParseException { public JsonElement next() throws JsonParseException {
if (eof) {
return null;
}
if (nextElement != null) {
JsonElement returnValue = nextElement;
nextElement = null;
return returnValue;
}
try { try {
JsonElement element = parser.parse(); JsonElement element = parser.parse();
return element; return element;
@ -68,10 +81,20 @@ public final class JsonParserAsync {
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;
return null; return null;
} else { } else {
throw e; throw e;
} }
} }
} }
public boolean hasNext() {
nextElement = next();
return nextElement != null;
}
public void remove() {
throw new UnsupportedOperationException();
}
} }

View File

@ -1,5 +1,8 @@
package com.google.gson; package com.google.gson;
import java.util.Enumeration;
import java.util.Iterator;
import junit.framework.TestCase; import junit.framework.TestCase;
/** /**
@ -11,9 +14,18 @@ public class JsonParserAsyncTest extends TestCase {
public void testParseTwoStrings() { public void testParseTwoStrings() {
JsonParserAsync parser = new JsonParserAsync("'one' 'two'"); JsonParserAsync parser = new JsonParserAsync("'one' 'two'");
String actualOne = parser.nextElement().getAsString(); String actualOne = parser.next().getAsString();
assertEquals("one", actualOne); assertEquals("one", actualOne);
String actualTwo = parser.nextElement().getAsString(); String actualTwo = parser.next().getAsString();
assertEquals("two", actualTwo); assertEquals("two", actualTwo);
} }
public void testIterator() {
Iterator<JsonElement> parser = new JsonParserAsync("'one' 'two'");
assertTrue(parser.hasNext());
assertEquals("one", parser.next().getAsString());
assertTrue(parser.hasNext());
assertEquals("two", parser.next().getAsString());
assertFalse(parser.hasNext());
}
} }

View File

@ -93,9 +93,9 @@ public class ReadersWritersTest extends TestCase {
writer.write(gson.toJson("two").toCharArray()); writer.write(gson.toJson("two").toCharArray());
CharArrayReader reader = new CharArrayReader(writer.toCharArray()); CharArrayReader reader = new CharArrayReader(writer.toCharArray());
JsonParserAsync parser = new JsonParserAsync(reader); JsonParserAsync parser = new JsonParserAsync(reader);
String actualOne = gson.fromJson(parser.nextElement(), String.class); String actualOne = gson.fromJson(parser.next(), String.class);
assertEquals("one", actualOne); assertEquals("one", actualOne);
String actualTwo = gson.fromJson(parser.nextElement(), String.class); String actualTwo = gson.fromJson(parser.next(), String.class);
assertEquals("two", actualTwo); assertEquals("two", actualTwo);
} }
@ -108,10 +108,11 @@ public class ReadersWritersTest extends TestCase {
writer.write(gson.toJson(expectedTwo).toCharArray()); writer.write(gson.toJson(expectedTwo).toCharArray());
CharArrayReader reader = new CharArrayReader(writer.toCharArray()); CharArrayReader reader = new CharArrayReader(writer.toCharArray());
JsonParserAsync parser = new JsonParserAsync(reader); JsonParserAsync parser = new JsonParserAsync(reader);
BagOfPrimitives actualOne = gson.fromJson(parser.nextElement(), BagOfPrimitives.class); BagOfPrimitives actualOne = gson.fromJson(parser.next(), BagOfPrimitives.class);
assertEquals("one", actualOne.stringValue); assertEquals("one", actualOne.stringValue);
BagOfPrimitives actualTwo = gson.fromJson(parser.nextElement(), BagOfPrimitives.class); BagOfPrimitives actualTwo = gson.fromJson(parser.next(), BagOfPrimitives.class);
assertEquals("two", actualTwo.stringValue); assertEquals("two", actualTwo.stringValue);
JsonElement jsonElement = parser.nextElement(); JsonElement jsonElement = parser.next();
assertNull(jsonElement);
} }
} }