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:
parent
be05420c6b
commit
32afd1a4e4
@ -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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -16,41 +16,58 @@
|
|||||||
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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A parser to parse Json into a parse tree of {@link JsonElement}s
|
* A parser to parse Json into a parse tree of {@link JsonElement}s
|
||||||
*
|
*
|
||||||
* @author Inderjeet Singh
|
* @author Inderjeet Singh
|
||||||
* @author Joel Leitch
|
* @author Joel Leitch
|
||||||
* @since 1.3
|
* @since 1.3
|
||||||
*/
|
*/
|
||||||
public final class JsonParser {
|
public final class JsonParser {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parses the specified JSON string into a parse tree
|
* Parses the specified JSON string into a parse tree
|
||||||
*
|
*
|
||||||
* @param json JSON text
|
* @param json JSON text
|
||||||
* @return a parse tree of {@link JsonElement}s corresponding to the specified JSON
|
* @return a parse tree of {@link JsonElement}s corresponding to the specified JSON
|
||||||
* @throws JsonParseException if the specified text is not valid JSON
|
* @throws JsonParseException if the specified text is not valid JSON
|
||||||
* @since 1.3
|
* @since 1.3
|
||||||
*/
|
*/
|
||||||
public JsonElement parse(String json) throws JsonSyntaxException {
|
public JsonElement parse(String json) throws JsonSyntaxException {
|
||||||
return parse(new StringReader(json));
|
return parse(new StringReader(json));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parses the specified JSON string into a parse tree
|
* Parses the specified JSON string into a parse tree
|
||||||
*
|
*
|
||||||
* @param json JSON text
|
* @param json JSON text
|
||||||
* @return a parse tree of {@link JsonElement}s corresponding to the specified JSON
|
* @return a parse tree of {@link JsonElement}s corresponding to the specified JSON
|
||||||
* @throws JsonParseException if the specified text is not valid JSON
|
* @throws JsonParseException if the specified text is not valid JSON
|
||||||
* @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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -18,26 +18,27 @@ 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}
|
||||||
*
|
*
|
||||||
* @author Inderjeet Singh
|
* @author Inderjeet Singh
|
||||||
*/
|
*/
|
||||||
public class JsonParserTest extends TestCase {
|
public class JsonParserTest extends TestCase {
|
||||||
|
|
||||||
private JsonParser parser;
|
private JsonParser parser;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void setUp() throws Exception {
|
protected void setUp() throws Exception {
|
||||||
super.setUp();
|
super.setUp();
|
||||||
parser = new JsonParser();
|
parser = new JsonParser();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testParseString() {
|
public void testParseString() {
|
||||||
String json = "{a:10,b:'c'}";
|
String json = "{a:10,b:'c'}";
|
||||||
JsonElement e = parser.parse(json);
|
JsonElement e = parser.parse(json);
|
||||||
@ -45,7 +46,7 @@ public class JsonParserTest extends TestCase {
|
|||||||
assertEquals(10, e.getAsJsonObject().get("a").getAsInt());
|
assertEquals(10, e.getAsJsonObject().get("a").getAsInt());
|
||||||
assertEquals("c", e.getAsJsonObject().get("b").getAsString());
|
assertEquals("c", e.getAsJsonObject().get("b").getAsString());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testParseEmptyString() {
|
public void testParseEmptyString() {
|
||||||
JsonElement e = parser.parse("\" \"");
|
JsonElement e = parser.parse("\" \"");
|
||||||
assertTrue(e.isJsonPrimitive());
|
assertTrue(e.isJsonPrimitive());
|
||||||
@ -56,12 +57,20 @@ public class JsonParserTest extends TestCase {
|
|||||||
JsonElement e = parser.parse(" ");
|
JsonElement e = parser.parse(" ");
|
||||||
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);
|
||||||
assertTrue(e.isJsonArray());
|
assertTrue(e.isJsonArray());
|
||||||
|
|
||||||
JsonArray array = e.getAsJsonArray();
|
JsonArray array = e.getAsJsonArray();
|
||||||
assertEquals("{}", array.get(0).toString());
|
assertEquals("{}", array.get(0).toString());
|
||||||
assertEquals(13, array.get(1).getAsInt());
|
assertEquals(13, array.get(1).getAsInt());
|
||||||
@ -75,16 +84,16 @@ public class JsonParserTest extends TestCase {
|
|||||||
assertEquals(10, e.getAsJsonObject().get("a").getAsInt());
|
assertEquals(10, e.getAsJsonObject().get("a").getAsInt());
|
||||||
assertEquals("c", e.getAsJsonObject().get("b").getAsString());
|
assertEquals("c", e.getAsJsonObject().get("b").getAsString());
|
||||||
}
|
}
|
||||||
|
|
||||||
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());
|
||||||
BagOfPrimitives expectedTwo = new BagOfPrimitives(2, 2, false, "two");
|
BagOfPrimitives expectedTwo = new BagOfPrimitives(2, 2, false, "two");
|
||||||
writer.write(gson.toJson(expectedTwo).toCharArray());
|
writer.write(gson.toJson(expectedTwo).toCharArray());
|
||||||
CharArrayReader reader = new CharArrayReader(writer.toCharArray());
|
CharArrayReader reader = new CharArrayReader(writer.toCharArray());
|
||||||
|
|
||||||
JsonReader parser = new JsonReader(reader);
|
JsonReader parser = new JsonReader(reader);
|
||||||
parser.setLenient(true);
|
parser.setLenient(true);
|
||||||
JsonElement element1 = Streams.parse(parser);
|
JsonElement element1 = Streams.parse(parser);
|
||||||
|
@ -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.
|
||||||
*
|
*
|
||||||
@ -141,22 +146,22 @@ public class PrimitiveTest extends TestCase {
|
|||||||
value = gson.fromJson("[true]", boolean.class);
|
value = gson.fromJson("[true]", boolean.class);
|
||||||
assertEquals(true, value);
|
assertEquals(true, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testNumberSerialization() {
|
public void testNumberSerialization() {
|
||||||
Number expected = 1L;
|
Number expected = 1L;
|
||||||
String json = gson.toJson(expected);
|
String json = gson.toJson(expected);
|
||||||
assertEquals(expected.toString(), json);
|
assertEquals(expected.toString(), json);
|
||||||
|
|
||||||
json = gson.toJson(expected, Number.class);
|
json = gson.toJson(expected, Number.class);
|
||||||
assertEquals(expected.toString(), json);
|
assertEquals(expected.toString(), json);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testNumberDeserialization() {
|
public void testNumberDeserialization() {
|
||||||
String json = "1";
|
String json = "1";
|
||||||
Number expected = new Integer(json);
|
Number expected = new Integer(json);
|
||||||
Number actual = gson.fromJson(json, Number.class);
|
Number actual = gson.fromJson(json, Number.class);
|
||||||
assertEquals(expected.intValue(), actual.intValue());
|
assertEquals(expected.intValue(), actual.intValue());
|
||||||
|
|
||||||
json = String.valueOf(Long.MAX_VALUE);
|
json = String.valueOf(Long.MAX_VALUE);
|
||||||
expected = new Long(json);
|
expected = new Long(json);
|
||||||
actual = gson.fromJson(json, Number.class);
|
actual = gson.fromJson(json, Number.class);
|
||||||
@ -182,7 +187,7 @@ public class PrimitiveTest extends TestCase {
|
|||||||
assertEquals("[-122.08]", gson.toJson(target, double[].class));
|
assertEquals("[-122.08]", gson.toJson(target, double[].class));
|
||||||
assertEquals("[-122.08]", gson.toJson(target, Double[].class));
|
assertEquals("[-122.08]", gson.toJson(target, Double[].class));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testDoubleAsStringRepresentationDeserialization() {
|
public void testDoubleAsStringRepresentationDeserialization() {
|
||||||
String doubleValue = "1.0043E+5";
|
String doubleValue = "1.0043E+5";
|
||||||
Double expected = Double.valueOf(doubleValue);
|
Double expected = Double.valueOf(doubleValue);
|
||||||
@ -192,7 +197,7 @@ public class PrimitiveTest extends TestCase {
|
|||||||
double actual1 = gson.fromJson(doubleValue, double.class);
|
double actual1 = gson.fromJson(doubleValue, double.class);
|
||||||
assertEquals(expected.doubleValue(), actual1);
|
assertEquals(expected.doubleValue(), actual1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testDoubleNoFractAsStringRepresentationDeserialization() {
|
public void testDoubleNoFractAsStringRepresentationDeserialization() {
|
||||||
String doubleValue = "1E+5";
|
String doubleValue = "1E+5";
|
||||||
Double expected = Double.valueOf(doubleValue);
|
Double expected = Double.valueOf(doubleValue);
|
||||||
@ -262,20 +267,20 @@ public class PrimitiveTest extends TestCase {
|
|||||||
BigDecimal actual = gson.fromJson("1.55", BigDecimal.class);
|
BigDecimal actual = gson.fromJson("1.55", BigDecimal.class);
|
||||||
assertEquals(expected, actual);
|
assertEquals(expected, actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testBigDecimalPreservePrecisionSerialization() {
|
public void testBigDecimalPreservePrecisionSerialization() {
|
||||||
String expectedValue = "1.000";
|
String expectedValue = "1.000";
|
||||||
BigDecimal obj = new BigDecimal(expectedValue);
|
BigDecimal obj = new BigDecimal(expectedValue);
|
||||||
String actualValue = gson.toJson(obj);
|
String actualValue = gson.toJson(obj);
|
||||||
|
|
||||||
assertEquals(expectedValue, actualValue);
|
assertEquals(expectedValue, actualValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testBigDecimalPreservePrecisionDeserialization() {
|
public void testBigDecimalPreservePrecisionDeserialization() {
|
||||||
String json = "1.000";
|
String json = "1.000";
|
||||||
BigDecimal expected = new BigDecimal(json);
|
BigDecimal expected = new BigDecimal(json);
|
||||||
BigDecimal actual = gson.fromJson(json, BigDecimal.class);
|
BigDecimal actual = gson.fromJson(json, BigDecimal.class);
|
||||||
|
|
||||||
assertEquals(expected, actual);
|
assertEquals(expected, actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -285,7 +290,7 @@ public class PrimitiveTest extends TestCase {
|
|||||||
BigDecimal actual = gson.fromJson(doubleValue, BigDecimal.class);
|
BigDecimal actual = gson.fromJson(doubleValue, BigDecimal.class);
|
||||||
assertEquals(expected, actual);
|
assertEquals(expected, actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testBigDecimalNoFractAsStringRepresentationDeserialization() {
|
public void testBigDecimalNoFractAsStringRepresentationDeserialization() {
|
||||||
String doubleValue = "5E+5";
|
String doubleValue = "5E+5";
|
||||||
BigDecimal expected = new BigDecimal(doubleValue);
|
BigDecimal expected = new BigDecimal(doubleValue);
|
||||||
@ -339,7 +344,7 @@ public class PrimitiveTest extends TestCase {
|
|||||||
fail("BigInteger can not be decimal values.");
|
fail("BigInteger can not be decimal values.");
|
||||||
} catch (JsonParseException expected) { }
|
} catch (JsonParseException expected) { }
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testOverridingDefaultPrimitiveSerialization() {
|
public void testOverridingDefaultPrimitiveSerialization() {
|
||||||
CrazyLongTypeAdapter typeAdapter = new CrazyLongTypeAdapter();
|
CrazyLongTypeAdapter typeAdapter = new CrazyLongTypeAdapter();
|
||||||
gson = new GsonBuilder()
|
gson = new GsonBuilder()
|
||||||
@ -349,7 +354,7 @@ public class PrimitiveTest extends TestCase {
|
|||||||
long value = 1L;
|
long value = 1L;
|
||||||
String serializedValue = gson.toJson(value);
|
String serializedValue = gson.toJson(value);
|
||||||
assertEquals(String.valueOf(value + CrazyLongTypeAdapter.DIFFERENCE), serializedValue);
|
assertEquals(String.valueOf(value + CrazyLongTypeAdapter.DIFFERENCE), serializedValue);
|
||||||
|
|
||||||
long deserializedValue = gson.fromJson(serializedValue, long.class);
|
long deserializedValue = gson.fromJson(serializedValue, long.class);
|
||||||
assertEquals(value, deserializedValue);
|
assertEquals(value, deserializedValue);
|
||||||
}
|
}
|
||||||
@ -357,54 +362,54 @@ public class PrimitiveTest extends TestCase {
|
|||||||
private String extractElementFromArray(String json) {
|
private String extractElementFromArray(String json) {
|
||||||
return json.substring(json.indexOf('[') + 1, json.indexOf(']'));
|
return json.substring(json.indexOf('[') + 1, json.indexOf(']'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testDoubleNaNSerializationNotSupportedByDefault() {
|
public void testDoubleNaNSerializationNotSupportedByDefault() {
|
||||||
try {
|
try {
|
||||||
double nan = Double.NaN;
|
double nan = Double.NaN;
|
||||||
gson.toJson(nan);
|
gson.toJson(nan);
|
||||||
fail("Gson should not accept NaN for serialization");
|
fail("Gson should not accept NaN for serialization");
|
||||||
} catch (IllegalArgumentException expected) {
|
} catch (IllegalArgumentException expected) {
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
gson.toJson(Double.NaN);
|
gson.toJson(Double.NaN);
|
||||||
fail("Gson should not accept NaN for serialization");
|
fail("Gson should not accept NaN for serialization");
|
||||||
} catch (IllegalArgumentException expected) {
|
} catch (IllegalArgumentException expected) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testDoubleNaNSerialization() {
|
public void testDoubleNaNSerialization() {
|
||||||
Gson gson = new GsonBuilder().serializeSpecialFloatingPointValues().create();
|
Gson gson = new GsonBuilder().serializeSpecialFloatingPointValues().create();
|
||||||
double nan = Double.NaN;
|
double nan = Double.NaN;
|
||||||
assertEquals("NaN", gson.toJson(nan));
|
assertEquals("NaN", gson.toJson(nan));
|
||||||
assertEquals("NaN", gson.toJson(Double.NaN));
|
assertEquals("NaN", gson.toJson(Double.NaN));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testDoubleNaNDeserialization() {
|
public void testDoubleNaNDeserialization() {
|
||||||
assertTrue(Double.isNaN(gson.fromJson("NaN", Double.class)));
|
assertTrue(Double.isNaN(gson.fromJson("NaN", Double.class)));
|
||||||
assertTrue(Double.isNaN(gson.fromJson("NaN", double.class)));
|
assertTrue(Double.isNaN(gson.fromJson("NaN", double.class)));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testFloatNaNSerializationNotSupportedByDefault() {
|
public void testFloatNaNSerializationNotSupportedByDefault() {
|
||||||
try {
|
try {
|
||||||
float nan = Float.NaN;
|
float nan = Float.NaN;
|
||||||
gson.toJson(nan);
|
gson.toJson(nan);
|
||||||
fail("Gson should not accept NaN for serialization");
|
fail("Gson should not accept NaN for serialization");
|
||||||
} catch (IllegalArgumentException expected) {
|
} catch (IllegalArgumentException expected) {
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
gson.toJson(Float.NaN);
|
gson.toJson(Float.NaN);
|
||||||
fail("Gson should not accept NaN for serialization");
|
fail("Gson should not accept NaN for serialization");
|
||||||
} catch (IllegalArgumentException expected) {
|
} catch (IllegalArgumentException expected) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testFloatNaNSerialization() {
|
public void testFloatNaNSerialization() {
|
||||||
Gson gson = new GsonBuilder().serializeSpecialFloatingPointValues().create();
|
Gson gson = new GsonBuilder().serializeSpecialFloatingPointValues().create();
|
||||||
float nan = Float.NaN;
|
float nan = Float.NaN;
|
||||||
assertEquals("NaN", gson.toJson(nan));
|
assertEquals("NaN", gson.toJson(nan));
|
||||||
assertEquals("NaN", gson.toJson(Float.NaN));
|
assertEquals("NaN", gson.toJson(Float.NaN));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testFloatNaNDeserialization() {
|
public void testFloatNaNDeserialization() {
|
||||||
assertTrue(Float.isNaN(gson.fromJson("NaN", Float.class)));
|
assertTrue(Float.isNaN(gson.fromJson("NaN", Float.class)));
|
||||||
assertTrue(Float.isNaN(gson.fromJson("NaN", float.class)));
|
assertTrue(Float.isNaN(gson.fromJson("NaN", float.class)));
|
||||||
@ -414,7 +419,7 @@ public class PrimitiveTest extends TestCase {
|
|||||||
try {
|
try {
|
||||||
gson.fromJson("NaN", BigDecimal.class);
|
gson.fromJson("NaN", BigDecimal.class);
|
||||||
fail("Gson should not accept NaN for deserialization by default.");
|
fail("Gson should not accept NaN for deserialization by default.");
|
||||||
} catch (JsonParseException expected) {
|
} catch (JsonParseException expected) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -423,131 +428,131 @@ public class PrimitiveTest extends TestCase {
|
|||||||
double infinity = Double.POSITIVE_INFINITY;
|
double infinity = Double.POSITIVE_INFINITY;
|
||||||
gson.toJson(infinity);
|
gson.toJson(infinity);
|
||||||
fail("Gson should not accept positive infinity for serialization by default.");
|
fail("Gson should not accept positive infinity for serialization by default.");
|
||||||
} catch (IllegalArgumentException expected) {
|
} catch (IllegalArgumentException expected) {
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
gson.toJson(Double.POSITIVE_INFINITY);
|
gson.toJson(Double.POSITIVE_INFINITY);
|
||||||
fail("Gson should not accept positive infinity for serialization by default.");
|
fail("Gson should not accept positive infinity for serialization by default.");
|
||||||
} catch (IllegalArgumentException expected) {
|
} catch (IllegalArgumentException expected) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testDoubleInfinitySerialization() {
|
public void testDoubleInfinitySerialization() {
|
||||||
Gson gson = new GsonBuilder().serializeSpecialFloatingPointValues().create();
|
Gson gson = new GsonBuilder().serializeSpecialFloatingPointValues().create();
|
||||||
double infinity = Double.POSITIVE_INFINITY;
|
double infinity = Double.POSITIVE_INFINITY;
|
||||||
assertEquals("Infinity", gson.toJson(infinity));
|
assertEquals("Infinity", gson.toJson(infinity));
|
||||||
assertEquals("Infinity", gson.toJson(Double.POSITIVE_INFINITY));
|
assertEquals("Infinity", gson.toJson(Double.POSITIVE_INFINITY));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testDoubleInfinityDeserialization() {
|
public void testDoubleInfinityDeserialization() {
|
||||||
assertTrue(Double.isInfinite(gson.fromJson("Infinity", Double.class)));
|
assertTrue(Double.isInfinite(gson.fromJson("Infinity", Double.class)));
|
||||||
assertTrue(Double.isInfinite(gson.fromJson("Infinity", double.class)));
|
assertTrue(Double.isInfinite(gson.fromJson("Infinity", double.class)));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testFloatInfinitySerializationNotSupportedByDefault() {
|
public void testFloatInfinitySerializationNotSupportedByDefault() {
|
||||||
try {
|
try {
|
||||||
float infinity = Float.POSITIVE_INFINITY;
|
float infinity = Float.POSITIVE_INFINITY;
|
||||||
gson.toJson(infinity);
|
gson.toJson(infinity);
|
||||||
fail("Gson should not accept positive infinity for serialization by default");
|
fail("Gson should not accept positive infinity for serialization by default");
|
||||||
} catch (IllegalArgumentException expected) {
|
} catch (IllegalArgumentException expected) {
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
gson.toJson(Float.POSITIVE_INFINITY);
|
gson.toJson(Float.POSITIVE_INFINITY);
|
||||||
fail("Gson should not accept positive infinity for serialization by default");
|
fail("Gson should not accept positive infinity for serialization by default");
|
||||||
} catch (IllegalArgumentException expected) {
|
} catch (IllegalArgumentException expected) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testFloatInfinitySerialization() {
|
public void testFloatInfinitySerialization() {
|
||||||
Gson gson = new GsonBuilder().serializeSpecialFloatingPointValues().create();
|
Gson gson = new GsonBuilder().serializeSpecialFloatingPointValues().create();
|
||||||
float infinity = Float.POSITIVE_INFINITY;
|
float infinity = Float.POSITIVE_INFINITY;
|
||||||
assertEquals("Infinity", gson.toJson(infinity));
|
assertEquals("Infinity", gson.toJson(infinity));
|
||||||
assertEquals("Infinity", gson.toJson(Float.POSITIVE_INFINITY));
|
assertEquals("Infinity", gson.toJson(Float.POSITIVE_INFINITY));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testFloatInfinityDeserialization() {
|
public void testFloatInfinityDeserialization() {
|
||||||
assertTrue(Float.isInfinite(gson.fromJson("Infinity", Float.class)));
|
assertTrue(Float.isInfinite(gson.fromJson("Infinity", Float.class)));
|
||||||
assertTrue(Float.isInfinite(gson.fromJson("Infinity", float.class)));
|
assertTrue(Float.isInfinite(gson.fromJson("Infinity", float.class)));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testBigDecimalInfinityDeserializationNotSupported() {
|
public void testBigDecimalInfinityDeserializationNotSupported() {
|
||||||
try {
|
try {
|
||||||
gson.fromJson("Infinity", BigDecimal.class);
|
gson.fromJson("Infinity", BigDecimal.class);
|
||||||
fail("Gson should not accept positive infinity for deserialization with BigDecimal");
|
fail("Gson should not accept positive infinity for deserialization with BigDecimal");
|
||||||
} catch (JsonParseException expected) {
|
} catch (JsonParseException expected) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testNegativeInfinitySerializationNotSupportedByDefault() {
|
public void testNegativeInfinitySerializationNotSupportedByDefault() {
|
||||||
try {
|
try {
|
||||||
double negativeInfinity = Double.NEGATIVE_INFINITY;
|
double negativeInfinity = Double.NEGATIVE_INFINITY;
|
||||||
gson.toJson(negativeInfinity);
|
gson.toJson(negativeInfinity);
|
||||||
fail("Gson should not accept negative infinity for serialization by default");
|
fail("Gson should not accept negative infinity for serialization by default");
|
||||||
} catch (IllegalArgumentException expected) {
|
} catch (IllegalArgumentException expected) {
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
gson.toJson(Double.NEGATIVE_INFINITY);
|
gson.toJson(Double.NEGATIVE_INFINITY);
|
||||||
fail("Gson should not accept negative infinity for serialization by default");
|
fail("Gson should not accept negative infinity for serialization by default");
|
||||||
} catch (IllegalArgumentException expected) {
|
} catch (IllegalArgumentException expected) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testNegativeInfinitySerialization() {
|
public void testNegativeInfinitySerialization() {
|
||||||
Gson gson = new GsonBuilder().serializeSpecialFloatingPointValues().create();
|
Gson gson = new GsonBuilder().serializeSpecialFloatingPointValues().create();
|
||||||
double negativeInfinity = Double.NEGATIVE_INFINITY;
|
double negativeInfinity = Double.NEGATIVE_INFINITY;
|
||||||
assertEquals("-Infinity", gson.toJson(negativeInfinity));
|
assertEquals("-Infinity", gson.toJson(negativeInfinity));
|
||||||
assertEquals("-Infinity", gson.toJson(Double.NEGATIVE_INFINITY));
|
assertEquals("-Infinity", gson.toJson(Double.NEGATIVE_INFINITY));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testNegativeInfinityDeserialization() {
|
public void testNegativeInfinityDeserialization() {
|
||||||
assertTrue(Double.isInfinite(gson.fromJson("-Infinity", double.class)));
|
assertTrue(Double.isInfinite(gson.fromJson("-Infinity", double.class)));
|
||||||
assertTrue(Double.isInfinite(gson.fromJson("-Infinity", Double.class)));
|
assertTrue(Double.isInfinite(gson.fromJson("-Infinity", Double.class)));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testNegativeInfinityFloatSerializationNotSupportedByDefault() {
|
public void testNegativeInfinityFloatSerializationNotSupportedByDefault() {
|
||||||
try {
|
try {
|
||||||
float negativeInfinity = Float.NEGATIVE_INFINITY;
|
float negativeInfinity = Float.NEGATIVE_INFINITY;
|
||||||
gson.toJson(negativeInfinity);
|
gson.toJson(negativeInfinity);
|
||||||
fail("Gson should not accept negative infinity for serialization by default");
|
fail("Gson should not accept negative infinity for serialization by default");
|
||||||
} catch (IllegalArgumentException expected) {
|
} catch (IllegalArgumentException expected) {
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
gson.toJson(Float.NEGATIVE_INFINITY);
|
gson.toJson(Float.NEGATIVE_INFINITY);
|
||||||
fail("Gson should not accept negative infinity for serialization by default");
|
fail("Gson should not accept negative infinity for serialization by default");
|
||||||
} catch (IllegalArgumentException expected) {
|
} catch (IllegalArgumentException expected) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testNegativeInfinityFloatSerialization() {
|
public void testNegativeInfinityFloatSerialization() {
|
||||||
Gson gson = new GsonBuilder().serializeSpecialFloatingPointValues().create();
|
Gson gson = new GsonBuilder().serializeSpecialFloatingPointValues().create();
|
||||||
float negativeInfinity = Float.NEGATIVE_INFINITY;
|
float negativeInfinity = Float.NEGATIVE_INFINITY;
|
||||||
assertEquals("-Infinity", gson.toJson(negativeInfinity));
|
assertEquals("-Infinity", gson.toJson(negativeInfinity));
|
||||||
assertEquals("-Infinity", gson.toJson(Float.NEGATIVE_INFINITY));
|
assertEquals("-Infinity", gson.toJson(Float.NEGATIVE_INFINITY));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testNegativeInfinityFloatDeserialization() {
|
public void testNegativeInfinityFloatDeserialization() {
|
||||||
assertTrue(Float.isInfinite(gson.fromJson("-Infinity", float.class)));
|
assertTrue(Float.isInfinite(gson.fromJson("-Infinity", float.class)));
|
||||||
assertTrue(Float.isInfinite(gson.fromJson("-Infinity", Float.class)));
|
assertTrue(Float.isInfinite(gson.fromJson("-Infinity", Float.class)));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testBigDecimalNegativeInfinityDeserializationNotSupported() {
|
public void testBigDecimalNegativeInfinityDeserializationNotSupported() {
|
||||||
try {
|
try {
|
||||||
gson.fromJson("-Infinity", BigDecimal.class);
|
gson.fromJson("-Infinity", BigDecimal.class);
|
||||||
fail("Gson should not accept positive infinity for deserialization");
|
fail("Gson should not accept positive infinity for deserialization");
|
||||||
} catch (JsonParseException expected) {
|
} catch (JsonParseException expected) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testLongAsStringSerialization() throws Exception {
|
public void testLongAsStringSerialization() throws Exception {
|
||||||
gson = new GsonBuilder().setLongSerializationPolicy(LongSerializationPolicy.STRING).create();
|
gson = new GsonBuilder().setLongSerializationPolicy(LongSerializationPolicy.STRING).create();
|
||||||
String result = gson.toJson(15L);
|
String result = gson.toJson(15L);
|
||||||
assertEquals("\"15\"", result);
|
assertEquals("\"15\"", result);
|
||||||
|
|
||||||
// Test with an integer and ensure its still a number
|
// Test with an integer and ensure its still a number
|
||||||
result = gson.toJson(2);
|
result = gson.toJson(2);
|
||||||
assertEquals("2", result);
|
assertEquals("2", result);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testLongAsStringDeserialization() throws Exception {
|
public void testLongAsStringDeserialization() throws Exception {
|
||||||
long value = gson.fromJson("\"15\"", long.class);
|
long value = gson.fromJson("\"15\"", long.class);
|
||||||
assertEquals(15, value);
|
assertEquals(15, value);
|
||||||
@ -556,12 +561,29 @@ public class PrimitiveTest extends TestCase {
|
|||||||
value = gson.fromJson("\"25\"", long.class);
|
value = gson.fromJson("\"25\"", long.class);
|
||||||
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);
|
||||||
assertFalse(result.equals('"' + target + '"'));
|
assertFalse(result.equals('"' + target + '"'));
|
||||||
|
|
||||||
gson = new GsonBuilder().disableHtmlEscaping().create();
|
gson = new GsonBuilder().disableHtmlEscaping().create();
|
||||||
result = gson.toJson(target);
|
result = gson.toJson(target);
|
||||||
assertTrue(result.equals('"' + target + '"'));
|
assertTrue(result.equals('"' + target + '"'));
|
||||||
@ -572,11 +594,11 @@ public class PrimitiveTest extends TestCase {
|
|||||||
ClassWithIntegerField target = gson.fromJson(json, ClassWithIntegerField.class);
|
ClassWithIntegerField target = gson.fromJson(json, ClassWithIntegerField.class);
|
||||||
assertEquals(10, target.i.intValue());
|
assertEquals(10, target.i.intValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class ClassWithIntegerField {
|
private static class ClassWithIntegerField {
|
||||||
Integer i;
|
Integer i;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testPrimitiveClassLiteral() {
|
public void testPrimitiveClassLiteral() {
|
||||||
assertEquals(1, gson.fromJson("1", int.class).intValue());
|
assertEquals(1, gson.fromJson("1", int.class).intValue());
|
||||||
assertEquals(1, gson.fromJson(new StringReader("1"), int.class).intValue());
|
assertEquals(1, gson.fromJson(new StringReader("1"), int.class).intValue());
|
||||||
|
Loading…
Reference in New Issue
Block a user