Fixed issue 58 by disabling the use of field value actual type for all cases

except when it is marked as Object.
This commit is contained in:
Inderjeet Singh 2008-10-14 18:55:59 +00:00
parent 646d94d420
commit 745c8e2a83
2 changed files with 10 additions and 7 deletions

View File

@ -124,15 +124,10 @@ final class JsonSerializationVisitor implements ObjectNavigator.Visitor {
}
} else {
Object fieldValue = getFieldValue(f, obj);
// See if the fieldValue has better type information than the specified typeOfF
// This takes care of situations where the field was declared as an Object, but the
// actual value contains something more specific. See Issue 54.
if (fieldValue != null && typeOfF instanceof Class) {
Class<?> classOfF = (Class<?>) typeOfF;
Class<?> actualClassOfF = fieldValue.getClass();
if (classOfF.isAssignableFrom(actualClassOfF)) {
typeOfF = actualClassOfF;
}
if (fieldValue != null && typeOfF == Object.class) {
typeOfF = fieldValue.getClass();
}
addAsChildOfObject(f, typeOfF, fieldValue);
}

View File

@ -159,6 +159,14 @@ public class CollectionTest extends TestCase {
assertEquals("[1,2,3,4,5,6,7,8,9]", gson.toJson(target));
}
@SuppressWarnings("unchecked")
public void testRawCollectionSerialization() {
BagOfPrimitives bag1 = new BagOfPrimitives();
Collection target = Arrays.asList(bag1, bag1);
String json = gson.toJson(target);
assertTrue(json.contains(bag1.getExpectedJson()));
}
public void testRawCollectionDeserializationNotAlllowed() {
String json = "[0,1,2,3,4,5,6,7,8,9]";
try {