From d3eda04f33ed9386140bb8c3066716916e2de831 Mon Sep 17 00:00:00 2001 From: Inderjeet Singh Date: Fri, 22 Oct 2010 16:06:59 +0000 Subject: [PATCH] Added support for deserializing from null input to a null value. Added toString() method to ObjectTypePair. --- gson/src/main/java/com/google/gson/Gson.java | 3 +++ gson/src/main/java/com/google/gson/ObjectTypePair.java | 7 ++++++- .../test/java/com/google/gson/functional/ObjectTest.java | 8 +++++++- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/gson/src/main/java/com/google/gson/Gson.java b/gson/src/main/java/com/google/gson/Gson.java index 2401ebc6..11c202aa 100644 --- a/gson/src/main/java/com/google/gson/Gson.java +++ b/gson/src/main/java/com/google/gson/Gson.java @@ -415,6 +415,9 @@ public final class Gson { */ @SuppressWarnings("unchecked") public T fromJson(String json, Type typeOfT) throws JsonParseException { + if (json == null) { + return null; + } StringReader reader = new StringReader(json); T target = (T) fromJson(reader, typeOfT); return target; diff --git a/gson/src/main/java/com/google/gson/ObjectTypePair.java b/gson/src/main/java/com/google/gson/ObjectTypePair.java index de91071b..a8c74fda 100644 --- a/gson/src/main/java/com/google/gson/ObjectTypePair.java +++ b/gson/src/main/java/com/google/gson/ObjectTypePair.java @@ -44,7 +44,12 @@ final class ObjectTypePair { Type getType() { return type; } - + + @Override + public String toString() { + return String.format("preserveType: %b, type: %s, obj: %s", preserveType, type, obj); + } + Pair getMatchingHandler( ParameterizedTypeHandlerMap handlers) { HANDLER handler = null; diff --git a/gson/src/test/java/com/google/gson/functional/ObjectTest.java b/gson/src/test/java/com/google/gson/functional/ObjectTest.java index cb6c4dae..8e48492f 100644 --- a/gson/src/test/java/com/google/gson/functional/ObjectTest.java +++ b/gson/src/test/java/com/google/gson/functional/ObjectTest.java @@ -140,11 +140,17 @@ public class ObjectTest extends TestCase { assertEquals("", gson.toJson(null)); } - public void testNullDeserialization() throws Exception { + public void testEmptyStringDeserialization() throws Exception { Object object = gson.fromJson("", Object.class); assertNull(object); } + public void testNullDeserialization() throws Exception { + String myNullObject = null; + Object object = gson.fromJson(myNullObject, Object.class); + assertNull(object); + } + public void testNullFieldsSerialization() throws Exception { Nested target = new Nested(new BagOfPrimitives(10, 20, false, "stringValue"), null); assertEquals(target.getExpectedJson(), gson.toJson(target));