Allow serialization of nulls in a "List<Object>" type.
This commit is contained in:
parent
e5ed1cc59a
commit
933a3e5150
@ -443,10 +443,14 @@ final class DefaultTypeAdapters {
|
||||
childGenericType = new TypeInfoCollection(typeOfSrc).getElementType();
|
||||
}
|
||||
for (Object child : src) {
|
||||
Type childType = (childGenericType == null || childGenericType == Object.class)
|
||||
? child.getClass() : childGenericType;
|
||||
JsonElement element = context.serialize(child, childType);
|
||||
array.add(element);
|
||||
if (child == null) {
|
||||
array.add(JsonNull.createJsonNull());
|
||||
} else {
|
||||
Type childType = (childGenericType == null || childGenericType == Object.class)
|
||||
? child.getClass() : childGenericType;
|
||||
JsonElement element = context.serialize(child, childType);
|
||||
array.add(element);
|
||||
}
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
@ -153,6 +153,17 @@ public class CollectionTest extends TestCase {
|
||||
Type type = new TypeToken<List<Object>>() {}.getType();
|
||||
assertEquals("[\"Hello\",\"World\"]", gson.toJson(target, type));
|
||||
}
|
||||
|
||||
public void testCollectionOfObjectWithNullSerialization() {
|
||||
List<Object> target = new ArrayList<Object>();
|
||||
target.add("Hello");
|
||||
target.add(null);
|
||||
target.add("World");
|
||||
assertEquals("[\"Hello\",null,\"World\"]", gson.toJson(target));
|
||||
|
||||
Type type = new TypeToken<List<Object>>() {}.getType();
|
||||
assertEquals("[\"Hello\",null,\"World\"]", gson.toJson(target, type));
|
||||
}
|
||||
|
||||
public void testCollectionOfStringsSerialization() {
|
||||
List<String> target = new ArrayList<String>();
|
||||
|
Loading…
Reference in New Issue
Block a user