Allow serialization of Object in collections. This used to be supported before restructuring the Collection type handling.

This commit is contained in:
Joel Leitch 2008-11-20 01:06:21 +00:00
parent 3df2d44e40
commit 2250afe825
3 changed files with 13 additions and 3 deletions

View File

@ -111,7 +111,7 @@
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
<workspace>
../eclipse-ws/
/Users/jleitch/eclipse/workspace/
</workspace>
<workspaceCodeStylesURL>
file:///${basedir}/../lib/gson-formatting-styles.xml

View File

@ -382,8 +382,8 @@ final class DefaultTypeAdapters {
childGenericType = new TypeInfoCollection(typeOfSrc).getElementType();
}
for (Object child : src) {
Type childType = (childGenericType == null) ?
childType = child.getClass() : childGenericType;
Type childType = (childGenericType == null || childGenericType == Object.class)
? child.getClass() : childGenericType;
JsonElement element = context.serialize(child, childType);
array.add(element);
}

View File

@ -142,6 +142,16 @@ public class CollectionTest extends TestCase {
}
}
public void testCollectionOfObjectSerialization() {
List<Object> target = new ArrayList<Object>();
target.add("Hello");
target.add("World");
assertEquals("[\"Hello\",\"World\"]", gson.toJson(target));
Type type = new TypeToken<List<Object>>() {}.getType();
assertEquals("[\"Hello\",\"World\"]", gson.toJson(target, type));
}
public void testCollectionOfStringsSerialization() {
List<String> target = new ArrayList<String>();
target.add("Hello");