Fixed Issue 54 to enable serialization of fields that are declared of type

Object.
This commit is contained in:
Inderjeet Singh 2008-10-13 21:12:41 +00:00
parent f6a278018e
commit 5631132892
2 changed files with 27 additions and 1 deletions

View File

@ -124,6 +124,16 @@ 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;
}
}
addAsChildOfObject(f, typeOfF, fieldValue);
}
}

View File

@ -361,6 +361,21 @@ public class ObjectTest extends TestCase {
assertEquals(target.getExpectedJson(), gson.toJson(target));
}
/**
* Tests that a class field with type Object can be serialized properly.
* See issue 54
*/
public void testClassWithObjectFieldSerialization() {
ClassWithObjectField obj = new ClassWithObjectField();
obj.member = "abc";
String json = gson.toJson(obj);
assertTrue(json.contains("abc"));
}
private static class ClassWithObjectField {
Object member;
}
public void testInnerClassSerialization() {
Parent p = new Parent();
Parent.Child c = p.new Child();
@ -371,7 +386,8 @@ public class ObjectTest extends TestCase {
public void testInnerClassDeserialization() {
final Parent p = new Parent();
Gson gson = new GsonBuilder().registerTypeAdapter(Parent.Child.class, new InstanceCreator<Parent.Child>() {
Gson gson = new GsonBuilder().registerTypeAdapter(
Parent.Child.class, new InstanceCreator<Parent.Child>() {
public Parent.Child createInstance(Type type) {
return p.new Child();
}