From cbc6b4d939202b6970f19fbe19e7f4d6624d59e4 Mon Sep 17 00:00:00 2001 From: Inderjeet Singh Date: Thu, 18 Dec 2008 20:00:27 +0000 Subject: [PATCH] Added checks to ensure that we do not serialize NaN or postiive or negative infinity for doubles. --- .../com/google/gson/DefaultTypeAdapters.java | 3 ++ .../google/gson/functional/PrimitiveTest.java | 51 +++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/gson/src/main/java/com/google/gson/DefaultTypeAdapters.java b/gson/src/main/java/com/google/gson/DefaultTypeAdapters.java index 186f4468..a7000571 100644 --- a/gson/src/main/java/com/google/gson/DefaultTypeAdapters.java +++ b/gson/src/main/java/com/google/gson/DefaultTypeAdapters.java @@ -664,6 +664,9 @@ final class DefaultTypeAdapters { private static class DoubleTypeAdapter implements InstanceCreator, JsonSerializer, JsonDeserializer { 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); } diff --git a/gson/src/test/java/com/google/gson/functional/PrimitiveTest.java b/gson/src/test/java/com/google/gson/functional/PrimitiveTest.java index 65a64643..7ccdd80f 100644 --- a/gson/src/test/java/com/google/gson/functional/PrimitiveTest.java +++ b/gson/src/test/java/com/google/gson/functional/PrimitiveTest.java @@ -348,4 +348,55 @@ public class PrimitiveTest extends TestCase { private String extractElementFromArray(String json) { 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) { + } + } }