Added a new API method in JsonParser to allow reading of multiple JSON objects on a stream asynchronously.
This commit is contained in:
parent
6b39f52f53
commit
0127891081
|
@ -4,7 +4,7 @@
|
|||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<version>1.3</version>
|
||||
<version>1.4</version>
|
||||
<inceptionYear>2008</inceptionYear>
|
||||
<name>Gson</name>
|
||||
<url>http://code.google.com/p/google-gson/</url>
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user