Broaden "default" handler condition in RuntimeDelegatingTypeHandler
import org.terasology.persistence.typeHandling.TypeHandler;
import org.terasology.persistence.typeHandling.TypeHandlerContext;
import org.terasology.persistence.typeHandling.TypeHandlerLibrary;
-import org.terasology.persistence.typeHandling.inMemory.PersistedMap;
import org.terasology.persistence.typeHandling.reflection.SerializationSandbox;
import org.terasology.reflection.TypeInfo;
import org.terasology.utilities.ReflectionUtil;
@@ -79,19 +78,26 @@ public PersistedData serializeNonNull(T value, PersistedDataSerializer serialize
if (!typeInfo.getType().equals(runtimeType)) {
Optional<TypeHandler<?>> runtimeTypeHandler = typeHandlerLibrary.getTypeHandler(runtimeType);
- chosenHandler = (TypeHandler<T>) runtimeTypeHandler
+ chosenHandler =
+ (TypeHandler<T>)
+ runtimeTypeHandler
.map(typeHandler -> {
if (delegateHandler == null) {
return typeHandler;
- } else if (!(typeHandler instanceof ObjectFieldMapTypeHandler)) {
+ }
+
+ if (!isDefaultTypeHandler(typeHandler)) {
if (typeHandler.getClass().equals(delegateHandler.getClass())) {
- // Both handlers are of same type, use delegateHandler
+ // Both handlers are of same type, use delegateHandler which
+ // might have more info
return delegateHandler;
}
// Custom handler for runtime type
return typeHandler;
- } else if (!(delegateHandler instanceof ObjectFieldMapTypeHandler)) {
+ }
+
+ if (!isDefaultTypeHandler(delegateHandler)) {
// Custom handler for specified type
return delegateHandler;
}
@@ -138,6 +144,14 @@ public PersistedData serializeNonNull(T value, PersistedDataSerializer serialize
return serializer.serialize(typeValuePersistedDataMap);
}
+ private boolean isDefaultTypeHandler(TypeHandler<?> typeHandler) {
+ return typeHandler instanceof ObjectFieldMapTypeHandler ||
+ typeHandler instanceof EnumTypeHandler ||
+ typeHandler instanceof CollectionTypeHandler ||
+ typeHandler instanceof StringMapTypeHandler ||
+ typeHandler instanceof ArrayTypeHandler;
+ }
+
private PersistedData serializeViaDelegate(T value, PersistedDataSerializer serializer) {
if (delegateHandler == null) {
LOGGER.error("Base type '{}' does not have a handler", typeInfo);
@@ -181,7 +195,8 @@ private Type getRuntimeTypeIfMoreSpecific(T value) {
return deserializeViaDelegate(data);
}
- TypeHandler<T> runtimeTypeHandler = (TypeHandler<T>) typeHandlerLibrary.getTypeHandler(typeToDeserializeAs.get())
+ TypeHandler<T> runtimeTypeHandler =
+ (TypeHandler<T>) typeHandlerLibrary.getTypeHandler(typeToDeserializeAs.get())
// To avoid compile errors in the orElseGet
.map(typeHandler -> (TypeHandler) typeHandler)
.orElseGet(() -> {
