Added checks to ensure that we do not serialize NaN or postiive or negative infinity for doubles.
This commit is contained in:
parent
0993d729e4
commit
cbc6b4d939
@ -664,6 +664,9 @@ final class DefaultTypeAdapters {
|
|||||||
private static class DoubleTypeAdapter
|
private static class DoubleTypeAdapter
|
||||||
implements InstanceCreator<Double>, JsonSerializer<Double>, JsonDeserializer<Double> {
|
implements InstanceCreator<Double>, JsonSerializer<Double>, JsonDeserializer<Double> {
|
||||||
public JsonElement serialize(Double src, Type typeOfSrc, JsonSerializationContext context) {
|
public JsonElement serialize(Double src, Type typeOfSrc, JsonSerializationContext context) {
|
||||||
|
if (Double.isNaN(src) || Double.isInfinite(src)) {
|
||||||
|
throw new IllegalArgumentException(src + " is not a valid double value as per JavaScript specification.");
|
||||||
|
}
|
||||||
return new JsonPrimitive(src);
|
return new JsonPrimitive(src);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -348,4 +348,55 @@ 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 testNaNSerializationNotSupported() {
|
||||||
|
try {
|
||||||
|
double d = Double.NaN;
|
||||||
|
gson.toJson(d);
|
||||||
|
fail("Gson should not accept NaN for serialization");
|
||||||
|
} catch (IllegalArgumentException expected) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testNaNDeserializationNotSupported() {
|
||||||
|
try {
|
||||||
|
String json = "NaN";
|
||||||
|
assertEquals(Double.NaN, gson.fromJson(json, Double.class));
|
||||||
|
} catch (JsonParseException expected) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testInfinitySerializationNotSupported() {
|
||||||
|
try {
|
||||||
|
double d = Double.POSITIVE_INFINITY;
|
||||||
|
gson.toJson(d);
|
||||||
|
fail("Gson should not accept positive infinity for serialization");
|
||||||
|
} catch (IllegalArgumentException expected) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testInfinityDeserializationNotSupported() {
|
||||||
|
try {
|
||||||
|
String json = "Infinity";
|
||||||
|
assertEquals(Double.POSITIVE_INFINITY, gson.fromJson(json, Double.class));
|
||||||
|
} catch (JsonParseException expected) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testNegativeInfinitySerializationNotSupported() {
|
||||||
|
try {
|
||||||
|
double d = Double.NEGATIVE_INFINITY;
|
||||||
|
gson.toJson(d);
|
||||||
|
fail("Gson should not accept positive infinity for serialization");
|
||||||
|
} catch (IllegalArgumentException expected) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testNegativeInfinityDeserializationNotSupported() {
|
||||||
|
try {
|
||||||
|
String json = "-Infinity";
|
||||||
|
assertEquals(Double.NEGATIVE_INFINITY, gson.fromJson(json, Double.class));
|
||||||
|
} catch (JsonParseException expected) {
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user