Restored this behavior for serialization: (as verified in CustomSerializerTest)
First preference: a type adapter registered for the runtime type Second preference: a type adapter registered for the declared type Third preference: reflective type adapter for the runtime type (if it is a sub class of the declared type) Fourth preference: reflective type adapter for the declared type
This commit is contained in:
parent
f7121ad87d
commit
57ea7ff9f3
|
@ -42,13 +42,28 @@ final class TypeAdapterRuntimeTypeWrapper<T> extends TypeAdapter<T> {
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@Override
|
@Override
|
||||||
public void write(JsonWriter writer, T value) throws IOException {
|
public void write(JsonWriter writer, T value) throws IOException {
|
||||||
TypeAdapter t = delegate;
|
// Order of preference for choosing type adapters
|
||||||
if (delegate instanceof ReflectiveTypeAdapter) {
|
// First preference: a type adapter registered for the runtime type
|
||||||
Type runtimeType = Reflection.getRuntimeTypeIfMoreSpecific(type, value);
|
// Second preference: a type adapter registered for the declared type
|
||||||
if (runtimeType != type) {
|
// Third preference: reflective type adapter for the runtime type (if it is a sub class of the declared type)
|
||||||
t = context.getAdapter(TypeToken.get(runtimeType));
|
// Fourth preference: reflective type adapter for the declared type
|
||||||
|
|
||||||
|
TypeAdapter chosen = delegate;
|
||||||
|
Type runtimeType = Reflection.getRuntimeTypeIfMoreSpecific(type, value);
|
||||||
|
if (runtimeType != type) {
|
||||||
|
TypeAdapter runtimeTypeAdapter = context.getAdapter(TypeToken.get(runtimeType));
|
||||||
|
if (!(runtimeTypeAdapter instanceof ReflectiveTypeAdapter)) {
|
||||||
|
// The user registered a type adapter for the runtime type, so we will use that
|
||||||
|
chosen = runtimeTypeAdapter;
|
||||||
|
} else if (!(delegate instanceof ReflectiveTypeAdapter)) {
|
||||||
|
// The user registered a type adapter for Base class, so we prefer it over the
|
||||||
|
// reflective type adapter for the runtime type
|
||||||
|
chosen = delegate;
|
||||||
|
} else {
|
||||||
|
// Use the type adapter for runtime type
|
||||||
|
chosen = runtimeTypeAdapter;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
t.write(writer, value);
|
chosen.write(writer, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user