diff --git a/gson/pom.xml b/gson/pom.xml
index dcf71bb3..10c8e608 100644
--- a/gson/pom.xml
+++ b/gson/pom.xml
@@ -4,7 +4,7 @@
com.google.code.gson
gson
jar
- 1.3
+ 1.4
2008
Gson
http://code.google.com/p/google-gson/
diff --git a/gson/src/main/java/com/google/gson/JsonParser.java b/gson/src/main/java/com/google/gson/JsonParser.java
index 9d3d1245..cec265f5 100755
--- a/gson/src/main/java/com/google/gson/JsonParser.java
+++ b/gson/src/main/java/com/google/gson/JsonParser.java
@@ -26,6 +26,21 @@ import java.io.StringReader;
* @since 1.3
*/
public final class JsonParser {
+
+ /**
+ * Interface to provide ability to read multiple {@link JsonElement}s from a stream
+ * asynchronously.
+ *
+ * @since 1.4
+ */
+ public interface AsyncReader {
+
+ /**
+ * Parse and return one {@link JsonElement}
+ * @since 1.4
+ */
+ public JsonElement readElement();
+ }
/**
* Parses the specified JSON string into a parse tree
@@ -50,7 +65,8 @@ public final class JsonParser {
public JsonElement parse(Reader json) throws JsonParseException {
try {
JsonParserJavacc parser = new JsonParserJavacc(json);
- return parser.parse();
+ JsonElement element = parser.parse();
+ return element;
} catch (TokenMgrError e) {
throw new JsonParseException("Failed parsing JSON source: " + json + " to Json", e);
} catch (ParseException e) {
@@ -61,4 +77,39 @@ public final class JsonParser {
throw new JsonParseException("Failed parsing JSON source: " + json + " to Json", e);
}
}
+
+ /**
+ * Returns {@link AsyncReader} to allow reading of multiple {@link JsonElement}s from the
+ * specified reader asynchronously.
+ *
+ * @param json The data stream containing JSON elements concatenated to each other.
+ * @return {@link AsyncReader} for reading {@link JsonElement}s asynchronously.
+ * @throws JsonParseException if the incoming stream is malformed JSON.
+ * @since 1.4
+ */
+ public AsyncReader parseAsync(Reader json) throws JsonParseException {
+ return new AsyncReaderJavacc(json);
+ }
+
+ private static class AsyncReaderJavacc implements AsyncReader {
+ private final JsonParserJavacc parser;
+ private AsyncReaderJavacc(Reader json) {
+ parser = new JsonParserJavacc(json);
+ }
+
+ public JsonElement readElement() {
+ try {
+ JsonElement element = parser.parse();
+ return element;
+ } catch (TokenMgrError e) {
+ throw new JsonParseException("Failed parsing JSON source to Json", e);
+ } catch (ParseException e) {
+ throw new JsonParseException("Failed parsing JSON source to Json", e);
+ } catch (StackOverflowError e) {
+ throw new JsonParseException("Failed parsing JSON source to Json", e);
+ } catch (OutOfMemoryError e) {
+ throw new JsonParseException("Failed parsing JSON source to Json", e);
+ }
+ }
+ }
}
diff --git a/gson/src/test/java/com/google/gson/JsonParserTest.java b/gson/src/test/java/com/google/gson/JsonParserTest.java
index 70a98975..b4537520 100644
--- a/gson/src/test/java/com/google/gson/JsonParserTest.java
+++ b/gson/src/test/java/com/google/gson/JsonParserTest.java
@@ -16,8 +16,12 @@
package com.google.gson;
+import com.google.gson.common.TestTypes.BagOfPrimitives;
+
import junit.framework.TestCase;
+import java.io.CharArrayReader;
+import java.io.CharArrayWriter;
import java.io.StringReader;
/**
@@ -50,4 +54,22 @@ public class JsonParserTest extends TestCase {
assertEquals(10, e.getAsJsonObject().get("a").getAsInt());
assertEquals("c", e.getAsJsonObject().get("b").getAsString());
}
+
+ public void testReadWriteTwoObjects() throws Exception {
+ Gson gson= new Gson();
+ CharArrayWriter writer= new CharArrayWriter();
+ BagOfPrimitives expectedOne = new BagOfPrimitives(1, 1, true, "one");
+ writer.write(gson.toJson(expectedOne).toCharArray());
+ BagOfPrimitives expectedTwo = new BagOfPrimitives(2, 2, false, "two");
+ writer.write(gson.toJson(expectedTwo).toCharArray());
+ CharArrayReader reader = new CharArrayReader(writer.toCharArray());
+
+ JsonParserJavacc parser = new JsonParserJavacc(reader);
+ JsonElement element1 = parser.parse();
+ JsonElement element2 = parser.parse();
+ BagOfPrimitives actualOne = gson.fromJson(element1, BagOfPrimitives.class);
+ assertEquals("one", actualOne.stringValue);
+ BagOfPrimitives actualTwo = gson.fromJson(element2, BagOfPrimitives.class);
+ assertEquals("two", actualTwo.stringValue);
+ }
}
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 85a7326f..8b5316f3 100644
--- a/gson/src/test/java/com/google/gson/functional/ReadersWritersTest.java
+++ b/gson/src/test/java/com/google/gson/functional/ReadersWritersTest.java
@@ -17,10 +17,16 @@ package com.google.gson.functional;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
+import com.google.gson.JsonParseException;
+import com.google.gson.JsonParser;
+import com.google.gson.JsonParser.AsyncReader;
import com.google.gson.common.TestTypes.BagOfPrimitives;
import junit.framework.TestCase;
+import java.io.CharArrayReader;
+import java.io.CharArrayWriter;
+import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
@@ -80,4 +86,34 @@ public class ReadersWritersTest extends TestCase {
Integer nullIntObject = gson.fromJson(reader, Integer.class);
assertNull(nullIntObject);
}
+
+ public void testReadWriteTwoStrings() throws IOException {
+ Gson gson= new Gson();
+ CharArrayWriter writer= new CharArrayWriter();
+ writer.write(gson.toJson("one").toCharArray());
+ writer.write(gson.toJson("two").toCharArray());
+ CharArrayReader reader = new CharArrayReader(writer.toCharArray());
+ JsonParser parser = new JsonParser();
+ AsyncReader asyncReader = parser.parseAsync(reader);
+ String actualOne = gson.fromJson(asyncReader.readElement(), String.class);
+ assertEquals("one", actualOne);
+ String actualTwo = gson.fromJson(asyncReader.readElement(), String.class);
+ assertEquals("two", actualTwo);
+ }
+
+ public void testReadWriteTwoObjects() throws IOException {
+ Gson gson= new Gson();
+ CharArrayWriter writer= new CharArrayWriter();
+ BagOfPrimitives expectedOne = new BagOfPrimitives(1, 1, true, "one");
+ writer.write(gson.toJson(expectedOne).toCharArray());
+ BagOfPrimitives expectedTwo = new BagOfPrimitives(2, 2, false, "two");
+ writer.write(gson.toJson(expectedTwo).toCharArray());
+ CharArrayReader reader = new CharArrayReader(writer.toCharArray());
+ JsonParser parser = new JsonParser();
+ AsyncReader asyncReader = parser.parseAsync(reader);
+ BagOfPrimitives actualOne = gson.fromJson(asyncReader.readElement(), BagOfPrimitives.class);
+ assertEquals("one", actualOne.stringValue);
+ BagOfPrimitives actualTwo = gson.fromJson(asyncReader.readElement(), BagOfPrimitives.class);
+ assertEquals("two", actualTwo.stringValue);
+ }
}