diff --git a/extras/src/main/java/com/google/gson/typeadapters/RuntimeTypeAdapterFactory.java b/extras/src/main/java/com/google/gson/typeadapters/RuntimeTypeAdapterFactory.java index 3e8aebcf60..a8c6368c28 100644 --- a/extras/src/main/java/com/google/gson/typeadapters/RuntimeTypeAdapterFactory.java +++ b/extras/src/main/java/com/google/gson/typeadapters/RuntimeTypeAdapterFactory.java @@ -16,10 +16,6 @@ package com.google.gson.typeadapters; -import java.io.IOException; -import java.util.LinkedHashMap; -import java.util.Map; - import com.google.gson.Gson; import com.google.gson.JsonElement; import com.google.gson.JsonObject; @@ -30,6 +26,10 @@ import com.google.gson.reflect.TypeToken; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonWriter; +import java.io.IOException; +import java.util.LinkedHashMap; +import java.util.Map; + /** * Adapts values whose runtime type may differ from their declaration type. This @@ -205,7 +205,7 @@ public RuntimeTypeAdapterFactory registerSubtype(Class type) { @Override public TypeAdapter create(Gson gson, TypeToken type) { - if (type.getRawType() != baseType) { + if (type == null || !baseType.isAssignableFrom(type.getRawType())) { return null; } diff --git a/extras/src/test/java/com/google/gson/typeadapters/RuntimeTypeAdapterFactoryTest.java b/extras/src/test/java/com/google/gson/typeadapters/RuntimeTypeAdapterFactoryTest.java index 8c62bef7f4..e58ee0f9c3 100644 --- a/extras/src/test/java/com/google/gson/typeadapters/RuntimeTypeAdapterFactoryTest.java +++ b/extras/src/test/java/com/google/gson/typeadapters/RuntimeTypeAdapterFactoryTest.java @@ -34,7 +34,9 @@ public void testRuntimeTypeAdapter() { CreditCard original = new CreditCard("Jesse", 234); assertEquals("{\"type\":\"CreditCard\",\"cvv\":234,\"ownerName\":\"Jesse\"}", - gson.toJson(original, BillingInstrument.class)); + //do not give the explicit typeOfSrc, because if this would be in a list + //or an attribute, there would also be no hint. See #712 + gson.toJson(original)); BillingInstrument deserialized = gson.fromJson( "{type:'CreditCard',cvv:234,ownerName:'Jesse'}", BillingInstrument.class); assertEquals("Jesse", deserialized.ownerName);