Fixing parsing of unquoted strings to be (somewhat) consistent with previous versions of Gson. The difference with this version is that Gson will throw a more specific exception rather than JsonParseException.

This commit is contained in:
Joel Leitch 2010-11-10 02:02:57 +00:00
parent be05420c6b
commit 32afd1a4e4
4 changed files with 149 additions and 81 deletions

View File

@ -17,7 +17,10 @@
package com.google.gson; package com.google.gson;
import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter; import com.google.gson.stream.JsonWriter;
import com.google.gson.stream.MalformedJsonException;
import java.io.IOException; import java.io.IOException;
import java.io.Reader; import java.io.Reader;
import java.io.StringReader; import java.io.StringReader;
@ -452,7 +455,9 @@ public final class Gson {
* @since 1.2 * @since 1.2
*/ */
public <T> T fromJson(Reader json, Class<T> classOfT) throws JsonSyntaxException, JsonIOException { public <T> T fromJson(Reader json, Class<T> classOfT) throws JsonSyntaxException, JsonIOException {
Object object = fromJson(new JsonReader(json), classOfT); JsonReader jsonReader = new JsonReader(json);
Object object = fromJson(jsonReader, classOfT);
assertFullConsumption(object, jsonReader);
return Primitives.wrap(classOfT).cast(object); return Primitives.wrap(classOfT).cast(object);
} }
@ -476,7 +481,22 @@ public final class Gson {
* @since 1.2 * @since 1.2
*/ */
public <T> T fromJson(Reader json, Type typeOfT) throws JsonIOException, JsonSyntaxException { public <T> T fromJson(Reader json, Type typeOfT) throws JsonIOException, JsonSyntaxException {
return this.<T>fromJson(new JsonReader(json), typeOfT); JsonReader jsonReader = new JsonReader(json);
T object = fromJson(jsonReader, typeOfT);
assertFullConsumption(object, jsonReader);
return object;
}
private static void assertFullConsumption(Object obj, JsonReader reader) {
try {
if (obj != null && reader.peek() != JsonToken.END_DOCUMENT) {
throw new JsonIOException("JSON document was not fully consumed.");
}
} catch (MalformedJsonException e) {
throw new JsonSyntaxException(e);
} catch (IOException e) {
throw new JsonIOException(e);
}
} }
/** /**

View File

@ -16,7 +16,11 @@
package com.google.gson; package com.google.gson;
import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.MalformedJsonException;
import java.io.EOFException; import java.io.EOFException;
import java.io.IOException;
import java.io.Reader; import java.io.Reader;
import java.io.StringReader; import java.io.StringReader;
@ -50,7 +54,20 @@ public final class JsonParser {
* @since 1.3 * @since 1.3
*/ */
public JsonElement parse(Reader json) throws JsonIOException, JsonSyntaxException { public JsonElement parse(Reader json) throws JsonIOException, JsonSyntaxException {
return parse(new JsonReader(json)); try {
JsonReader jsonReader = new JsonReader(json);
JsonElement element = parse(jsonReader);
if (!element.isJsonNull() && jsonReader.peek() != JsonToken.END_DOCUMENT) {
throw new JsonSyntaxException("Did not consume the entire document.");
}
return element;
} catch (MalformedJsonException e) {
throw new JsonSyntaxException(e);
} catch (IOException e) {
throw new JsonIOException(e);
} catch (NumberFormatException e) {
throw new JsonSyntaxException(e);
}
} }
/** /**

View File

@ -18,10 +18,12 @@ package com.google.gson;
import com.google.gson.common.TestTypes.BagOfPrimitives; import com.google.gson.common.TestTypes.BagOfPrimitives;
import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonReader;
import junit.framework.TestCase;
import java.io.CharArrayReader; import java.io.CharArrayReader;
import java.io.CharArrayWriter; import java.io.CharArrayWriter;
import java.io.StringReader; import java.io.StringReader;
import junit.framework.TestCase;
/** /**
* Unit test for {@link JsonParser} * Unit test for {@link JsonParser}
@ -29,7 +31,6 @@ import junit.framework.TestCase;
* @author Inderjeet Singh * @author Inderjeet Singh
*/ */
public class JsonParserTest extends TestCase { public class JsonParserTest extends TestCase {
private JsonParser parser; private JsonParser parser;
@Override @Override
@ -57,6 +58,14 @@ public class JsonParserTest extends TestCase {
assertTrue(e.isJsonNull()); assertTrue(e.isJsonNull());
} }
public void testParseUnquotedStringSentence() {
String unquotedSentence = "Test is a test..blah blah";
try {
parser.parse(unquotedSentence);
fail();
} catch (JsonSyntaxException expected) { }
}
public void testParseMixedArray() { public void testParseMixedArray() {
String json = "[{},13,\"stringValue\"]"; String json = "[{},13,\"stringValue\"]";
JsonElement e = parser.parse(json); JsonElement e = parser.parse(json);
@ -77,7 +86,7 @@ public class JsonParserTest extends TestCase {
} }
public void testReadWriteTwoObjects() throws Exception { public void testReadWriteTwoObjects() throws Exception {
Gson gson= new Gson(); Gson gson = new Gson();
CharArrayWriter writer= new CharArrayWriter(); CharArrayWriter writer= new CharArrayWriter();
BagOfPrimitives expectedOne = new BagOfPrimitives(1, 1, true, "one"); BagOfPrimitives expectedOne = new BagOfPrimitives(1, 1, true, "one");
writer.write(gson.toJson(expectedOne).toCharArray()); writer.write(gson.toJson(expectedOne).toCharArray());

View File

@ -16,15 +16,20 @@
package com.google.gson.functional; package com.google.gson.functional;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSyntaxException;
import com.google.gson.LongSerializationPolicy;
import com.google.gson.common.TestTypes.CrazyLongTypeAdapter;
import junit.framework.TestCase;
import java.io.StringReader; import java.io.StringReader;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.BigInteger; import java.math.BigInteger;
import com.google.gson.*;
import junit.framework.TestCase;
import com.google.gson.common.TestTypes.CrazyLongTypeAdapter;
/** /**
* Functional tests for Json primitive values: integers, and floating point numbers. * Functional tests for Json primitive values: integers, and floating point numbers.
* *
@ -557,6 +562,23 @@ public class PrimitiveTest extends TestCase {
assertEquals(25, value); assertEquals(25, value);
} }
public void testQuotedStringSerializationAndDeserialization() throws Exception {
String value = "String Blah Blah Blah...1, 2, 3";
String serializedForm = gson.toJson(value);
assertEquals("\"" + value + "\"", serializedForm);
String actual = gson.fromJson(serializedForm, String.class);
assertEquals(value, actual);
}
public void testUnquotedStringDeserialization() throws Exception {
String value = "String Blah Blah Blah...1, 2, 3";
try {
gson.fromJson(value, String.class);
fail();
} catch (JsonSyntaxException expected) { }
}
public void testHtmlCharacterSerialization() throws Exception { public void testHtmlCharacterSerialization() throws Exception {
String target = "<script>var a = 12;</script>"; String target = "<script>var a = 12;</script>";
String result = gson.toJson(target); String result = gson.toJson(target);