Parity with Gson behavior where we use runtime type of an object while serializing instead of the declared type of the field.

This commit is contained in:
Inderjeet Singh 2011-08-04 22:27:25 +00:00
parent 2813385c33
commit 5fc2db9e72

View File

@ -25,8 +25,6 @@ import java.lang.reflect.TypeVariable;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; 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.$Gson$Types;
import com.google.gson.internal.UnsafeAllocator; import com.google.gson.internal.UnsafeAllocator;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
@ -138,10 +136,11 @@ public final class ReflectiveTypeAdapter<T> extends TypeAdapter<T> {
} }
private static Type getMoreSpecificType(Type type, Object obj, Object fieldValue) { private static Type getMoreSpecificType(Type type, Object obj, Object fieldValue) {
if (obj != null && (Object.class == type || type instanceof TypeVariable)) { if (obj == null || fieldValue == null) {
if (fieldValue != null) { return type;
type = fieldValue.getClass(); }
} if (type == Object.class || type instanceof TypeVariable || type instanceof Class<?>) {
type = (Class<?>) fieldValue.getClass();
} }
return type; return type;
} }