From 5fc2db9e7266701959129e88aa7ed8baeb493adc Mon Sep 17 00:00:00 2001 From: Inderjeet Singh Date: Thu, 4 Aug 2011 22:27:25 +0000 Subject: [PATCH] Parity with Gson behavior where we use runtime type of an object while serializing instead of the declared type of the field. --- .../gson/internal/bind/ReflectiveTypeAdapter.java | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/gson/src/main/java/com/google/gson/internal/bind/ReflectiveTypeAdapter.java b/gson/src/main/java/com/google/gson/internal/bind/ReflectiveTypeAdapter.java index 9ffc9b9d..421cac3c 100644 --- a/gson/src/main/java/com/google/gson/internal/bind/ReflectiveTypeAdapter.java +++ b/gson/src/main/java/com/google/gson/internal/bind/ReflectiveTypeAdapter.java @@ -25,8 +25,6 @@ import java.lang.reflect.TypeVariable; import java.util.LinkedHashMap; import java.util.Map; -import com.google.gson.FieldAttributes; -import com.google.gson.FieldAttributesTest; import com.google.gson.internal.$Gson$Types; import com.google.gson.internal.UnsafeAllocator; import com.google.gson.reflect.TypeToken; @@ -138,10 +136,11 @@ public final class ReflectiveTypeAdapter extends TypeAdapter { } private static Type getMoreSpecificType(Type type, Object obj, Object fieldValue) { - if (obj != null && (Object.class == type || type instanceof TypeVariable)) { - if (fieldValue != null) { - type = fieldValue.getClass(); - } + if (obj == null || fieldValue == null) { + return type; + } + if (type == Object.class || type instanceof TypeVariable || type instanceof Class) { + type = (Class) fieldValue.getClass(); } return type; }