From d61e754fb66b2c9adeb681bad156efc14667b052 Mon Sep 17 00:00:00 2001 From: Inderjeet Singh Date: Fri, 10 Oct 2008 22:31:19 +0000 Subject: [PATCH] Added tests for explicit serialization of null array, collection and string fields with serializeNulls set to true. --- .../functional/NullObjectAndFieldTest.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) 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 4953651e..eb9107b0 100755 --- a/gson/src/test/java/com/google/gson/functional/NullObjectAndFieldTest.java +++ b/gson/src/test/java/com/google/gson/functional/NullObjectAndFieldTest.java @@ -29,6 +29,7 @@ import com.google.gson.common.TestTypes.ClassWithObjects; import junit.framework.TestCase; import java.lang.reflect.Type; +import java.util.Collection; /** * Functional tests for the different cases for serializing (or ignoring) null fields and object. @@ -73,6 +74,33 @@ public class NullObjectAndFieldTest extends TestCase { ClassWithObjects target = gson.fromJson("{\"bag\":null}", ClassWithObjects.class); assertNull(target.bag); } + + public void testExplicitSerializationOfNullArrayMembers() { + Gson gson = gsonBuilder.create(); + ClassWithMembers target = new ClassWithMembers(); + String json = gson.toJson(target); + assertTrue(json.contains("\"array\":null")); + } + + public void testExplicitSerializationOfNullCollectionMembers() { + Gson gson = gsonBuilder.create(); + ClassWithMembers target = new ClassWithMembers(); + String json = gson.toJson(target); + assertTrue(json.contains("\"col\":null")); + } + + public void testExplicitSerializationOfNullStringMembers() { + Gson gson = gsonBuilder.create(); + ClassWithMembers target = new ClassWithMembers(); + String json = gson.toJson(target); + assertTrue(json.contains("\"str\":null")); + } + + static class ClassWithMembers { + String str; + int[] array; + Collection col; + } public void testCustomSerializationOfNulls() { gsonBuilder.registerTypeAdapter(ClassWithObjects.class, new ClassWithObjectsSerializer()); @@ -91,4 +119,5 @@ public class NullObjectAndFieldTest extends TestCase { return obj; } } + }