From f7121ad87d2916c72a854f746b37d62aead1ff0b Mon Sep 17 00:00:00 2001 From: Inderjeet Singh Date: Fri, 12 Aug 2011 01:59:22 +0000 Subject: [PATCH] Restored this behavior: If a type adapter is registered for a base class, then a field of that type is serialized with the type adapter instead of using the run-time type. This fixes: CustomSerializerTest.testBaseClassSerializerInvokedForBaseClassFieldsHoldingSubClassInstances --- .../internal/bind/TypeAdapterRuntimeTypeWrapper.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/gson/src/main/java/com/google/gson/internal/bind/TypeAdapterRuntimeTypeWrapper.java b/gson/src/main/java/com/google/gson/internal/bind/TypeAdapterRuntimeTypeWrapper.java index 1a1f7d84..337b737e 100644 --- a/gson/src/main/java/com/google/gson/internal/bind/TypeAdapterRuntimeTypeWrapper.java +++ b/gson/src/main/java/com/google/gson/internal/bind/TypeAdapterRuntimeTypeWrapper.java @@ -42,9 +42,13 @@ final class TypeAdapterRuntimeTypeWrapper extends TypeAdapter { @SuppressWarnings("unchecked") @Override public void write(JsonWriter writer, T value) throws IOException { - Type runtimeType = Reflection.getRuntimeTypeIfMoreSpecific(type, value); - TypeAdapter t = runtimeType != type ? - context.getAdapter(TypeToken.get(runtimeType)) : delegate; + TypeAdapter t = delegate; + if (delegate instanceof ReflectiveTypeAdapter) { + Type runtimeType = Reflection.getRuntimeTypeIfMoreSpecific(type, value); + if (runtimeType != type) { + t = context.getAdapter(TypeToken.get(runtimeType)); + } + } t.write(writer, value); } }