From 536a968b32eede34554121f75c72b2a658fc442e Mon Sep 17 00:00:00 2001 From: Inderjeet Singh Date: Tue, 6 Oct 2009 17:10:52 +0000 Subject: [PATCH] Disabled escaping of single quote since it is valid for JSON (though invalid for JavaScript). Added null checks in contexts since the custom type adapter can pass nulls. --- .../main/java/com/google/gson/Escaper.java | 6 ++-- .../JsonDeserializationContextDefault.java | 6 ++-- .../gson/JsonSerializationContextDefault.java | 3 ++ .../java/com/google/gson/EscaperTest.java | 2 +- .../functional/NullObjectAndFieldTest.java | 31 ++++++++++++++++++- 5 files changed, 41 insertions(+), 7 deletions(-) diff --git a/gson/src/main/java/com/google/gson/Escaper.java b/gson/src/main/java/com/google/gson/Escaper.java index 43913aa7..a9e81261 100644 --- a/gson/src/main/java/com/google/gson/Escaper.java +++ b/gson/src/main/java/com/google/gson/Escaper.java @@ -54,8 +54,10 @@ class Escaper { htmlEscapeSet.add('>'); htmlEscapeSet.add('&'); htmlEscapeSet.add('='); - htmlEscapeSet.add('\''); - // htmlEscapeSet.add('/'); -- Removing slash for now since it causes some incompatibilities + // Removing ' for now since it is a valid character in JSON, but not javascript + // When enabling this, remember to enable the test EscaperTest.disable_testSingleQuoteEscaping +// htmlEscapeSet.add('\''); +// htmlEscapeSet.add('/'); -- Removing slash for now since it causes some incompatibilities HTML_ESCAPE_CHARS = Collections.unmodifiableSet(htmlEscapeSet); } diff --git a/gson/src/main/java/com/google/gson/JsonDeserializationContextDefault.java b/gson/src/main/java/com/google/gson/JsonDeserializationContextDefault.java index 8a5c9ef5..7eb7179a 100644 --- a/gson/src/main/java/com/google/gson/JsonDeserializationContextDefault.java +++ b/gson/src/main/java/com/google/gson/JsonDeserializationContextDefault.java @@ -43,14 +43,14 @@ final class JsonDeserializationContextDefault implements JsonDeserializationCont @SuppressWarnings("unchecked") public T deserialize(JsonElement json, Type typeOfT) throws JsonParseException { - if (json.isJsonArray()) { + if (json == null || json.isJsonNull()) { + return null; + } else if (json.isJsonArray()) { return (T) fromJsonArray(typeOfT, json.getAsJsonArray(), this); } else if (json.isJsonObject()) { return (T) fromJsonObject(typeOfT, json.getAsJsonObject(), this); } else if (json.isJsonPrimitive()) { return (T) fromJsonPrimitive(typeOfT, json.getAsJsonPrimitive(), this); - } else if (json.isJsonNull()) { - return null; } else { throw new JsonParseException("Failed parsing JSON source: " + json + " to Json"); } diff --git a/gson/src/main/java/com/google/gson/JsonSerializationContextDefault.java b/gson/src/main/java/com/google/gson/JsonSerializationContextDefault.java index 6e906f53..a4c9e671 100644 --- a/gson/src/main/java/com/google/gson/JsonSerializationContextDefault.java +++ b/gson/src/main/java/com/google/gson/JsonSerializationContextDefault.java @@ -39,6 +39,9 @@ final class JsonSerializationContextDefault implements JsonSerializationContext } public JsonElement serialize(Object src) { + if (src == null) { + return JsonNull.createJsonNull(); + } return serialize(src, src.getClass()); } diff --git a/gson/src/test/java/com/google/gson/EscaperTest.java b/gson/src/test/java/com/google/gson/EscaperTest.java index 73fa54fa..8dde9d75 100644 --- a/gson/src/test/java/com/google/gson/EscaperTest.java +++ b/gson/src/test/java/com/google/gson/EscaperTest.java @@ -65,7 +65,7 @@ public class EscaperTest extends TestCase { assertEquals("123\\\"456", escapedString); } - public void testSingleQuoteEscaping() throws Exception { + public void disable_testSingleQuoteEscaping() throws Exception { String containsQuote = "123'456"; String escapedString = escapeHtmlChar.escapeJsonString(containsQuote); assertEquals("123\\'456", escapedString); diff --git a/gson/src/test/java/com/google/gson/functional/NullObjectAndFieldTest.java b/gson/src/test/java/com/google/gson/functional/NullObjectAndFieldTest.java index d8bbd8d6..0a5984a0 100755 --- a/gson/src/test/java/com/google/gson/functional/NullObjectAndFieldTest.java +++ b/gson/src/test/java/com/google/gson/functional/NullObjectAndFieldTest.java @@ -21,6 +21,8 @@ import com.google.gson.GsonBuilder; import com.google.gson.JsonElement; import com.google.gson.JsonNull; import com.google.gson.JsonObject; +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; import com.google.gson.JsonSerializationContext; import com.google.gson.JsonSerializer; import com.google.gson.common.TestTypes.BagOfPrimitives; @@ -173,7 +175,34 @@ public class NullObjectAndFieldTest extends TestCase { ObjectWithField obj = gson.fromJson(json, ObjectWithField.class); assertNull(obj.value); } - + + public void testCustomTypeAdapterPassesNullSerialization() { + Gson gson = new GsonBuilder() + .registerTypeAdapter(ObjectWithField.class, new JsonSerializer() { + public JsonElement serialize(ObjectWithField src, Type typeOfSrc, + JsonSerializationContext context) { + return context.serialize(null); + } + }).create(); + ObjectWithField target = new ObjectWithField(); + target.value = "value1"; + String json = gson.toJson(target); + assertFalse(json.contains("value1")); + } + + public void testCustomTypeAdapterPassesNullDesrialization() { + Gson gson = new GsonBuilder() + .registerTypeAdapter(ObjectWithField.class, new JsonDeserializer() { + public ObjectWithField deserialize(JsonElement json, Type type, + JsonDeserializationContext context) { + return context.deserialize(null, type); + } + }).create(); + String json = "{value:'value1'}"; + ObjectWithField target = gson.fromJson(json, ObjectWithField.class); + assertFalse("value1".equals(target.value)); + } + private static class ObjectWithField { String value = ""; }