Use runtime type if declared type handler is missing during serialization
@@ -71,7 +71,7 @@ public PersistedData serializeNonNull(T value, PersistedDataSerializer serialize
}
TypeHandler<T> chosenHandler = delegateHandler;
- Class<?> runtimeClass = getRuntimeTypeIfMoreSpecific(typeInfo, value);
+ Class<?> runtimeClass = getRuntimeTypeIfMoreSpecific(value);
if (!typeInfo.getRawType().equals(runtimeClass)) {
Optional<TypeHandler<?>> runtimeTypeHandler = typeHandlerLibrary.getTypeHandler((Type) runtimeClass);
@@ -120,7 +120,7 @@ public PersistedData serializeNonNull(T value, PersistedDataSerializer serialize
return serializer.serialize(typeValuePersistedDataMap);
}
- private static <T> Class<?> getRuntimeTypeIfMoreSpecific(TypeInfo<T> typeInfo, T value) {
+ private Class<?> getRuntimeTypeIfMoreSpecific(T value) {
if (value == null) {
return typeInfo.getRawType();
}
@@ -133,6 +133,9 @@ public PersistedData serializeNonNull(T value, PersistedDataSerializer serialize
} else if (typeInfo.getType() instanceof Class) {
// If given type is a simple class, use more specific runtime type
return runtimeClass;
+ } else if (delegateHandler == null) {
+ // If we can't find a handler for the declared type, use the runtime type
+ return runtimeClass;
}
// Given type has more information than runtime type, use that
