diff --git a/gson/src/main/java/com/google/gson/ObjectTypePair.java b/gson/src/main/java/com/google/gson/ObjectTypePair.java index 6dddd409..afc60060 100644 --- a/gson/src/main/java/com/google/gson/ObjectTypePair.java +++ b/gson/src/main/java/com/google/gson/ObjectTypePair.java @@ -79,6 +79,13 @@ final class ObjectTypePair { return new ObjectTypePair(obj, actualType, preserveType); } + Type getMoreSpecificType() { + if (preserveType || obj == null) { + return type; + } + return getActualTypeIfMoreSpecific(type, obj.getClass()); + } + // This takes care of situations where the field was declared as an Object, but the // actual value contains something more specific. See Issue 54. // TODO (inder): This solution will not work if the field is of a generic type, but diff --git a/gson/src/main/java/com/google/gson/ReflectingFieldNavigator.java b/gson/src/main/java/com/google/gson/ReflectingFieldNavigator.java index 20424052..bf4f79e6 100644 --- a/gson/src/main/java/com/google/gson/ReflectingFieldNavigator.java +++ b/gson/src/main/java/com/google/gson/ReflectingFieldNavigator.java @@ -54,9 +54,7 @@ final class ReflectingFieldNavigator { * @param visitor the visitor to visit each field with */ void visitFieldsReflectively(ObjectTypePair objTypePair, Visitor visitor) { - ObjectTypePair currObjTypePair = objTypePair.toMoreSpecificType(); - Class topLevelClass = Types.getRawType(currObjTypePair.type); - for (Class curr : getInheritanceHierarchy(currObjTypePair.type)) { + for (Class curr : getInheritanceHierarchy(objTypePair.getMoreSpecificType())) { navigateClassFields(objTypePair.getObject(), objTypePair.type, curr, visitor); } }