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();
|
childGenericType = new TypeInfoCollection(typeOfSrc).getElementType();
|
||||||
}
|
}
|
||||||
for (Object child : src) {
|
for (Object child : src) {
|
||||||
Type childType = (childGenericType == null || childGenericType == Object.class)
|
if (child == null) {
|
||||||
? child.getClass() : childGenericType;
|
array.add(JsonNull.createJsonNull());
|
||||||
JsonElement element = context.serialize(child, childType);
|
} else {
|
||||||
array.add(element);
|
Type childType = (childGenericType == null || childGenericType == Object.class)
|
||||||
|
? child.getClass() : childGenericType;
|
||||||
|
JsonElement element = context.serialize(child, childType);
|
||||||
|
array.add(element);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
|
@ -154,6 +154,17 @@ public class CollectionTest extends TestCase {
|
|||||||
assertEquals("[\"Hello\",\"World\"]", gson.toJson(target, type));
|
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() {
|
public void testCollectionOfStringsSerialization() {
|
||||||
List<String> target = new ArrayList<String>();
|
List<String> target = new ArrayList<String>();
|
||||||
target.add("Hello");
|
target.add("Hello");
|
||||||
|
Loading…
Reference in New Issue
Block a user