Added tests for explicit serialization of null array, collection and string

fields with serializeNulls set to true.
This commit is contained in:
Inderjeet Singh 2008-10-10 22:31:19 +00:00
parent d74ecbfe82
commit d61e754fb6

View File

@ -29,6 +29,7 @@ import com.google.gson.common.TestTypes.ClassWithObjects;
import junit.framework.TestCase; import junit.framework.TestCase;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.Collection;
/** /**
* Functional tests for the different cases for serializing (or ignoring) null fields and object. * 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); ClassWithObjects target = gson.fromJson("{\"bag\":null}", ClassWithObjects.class);
assertNull(target.bag); 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<String> col;
}
public void testCustomSerializationOfNulls() { public void testCustomSerializationOfNulls() {
gsonBuilder.registerTypeAdapter(ClassWithObjects.class, new ClassWithObjectsSerializer()); gsonBuilder.registerTypeAdapter(ClassWithObjects.class, new ClassWithObjectsSerializer());
@ -91,4 +119,5 @@ public class NullObjectAndFieldTest extends TestCase {
return obj; return obj;
} }
} }
} }