Fix unsupported op exception when serializing objects with nested objects

Signed-off-by: Irmo van den Berge <irmo.vandenberge@ziggo.nl>
This commit is contained in:
Irmo van den Berge committed 2023-08-06 15:59:37 +02:00
1 parent 5e17e63759
commit d5cc3612e5
1 file changed
+20 -1
@@ -8,6 +8,7 @@ import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
@@ -1144,10 +1145,28 @@ public class LogicUtil {
return Collections.emptyMap();
}
Map<String, Object> values = serializable.serialize();
boolean cloned = false;
for (Map.Entry<String, Object> entry : values.entrySet()) {
Object value = entry.getValue();
if (value instanceof ConfigurationSerializable) {
entry.setValue(serializeDeep((ConfigurationSerializable) value));
Map<String, Object> serialized = serializeDeep((ConfigurationSerializable) value);
if (!cloned) {
if (values instanceof com.google.common.collect.ImmutableMap) {
// Optimization: avoid UnsupportedOps if we know this is the case
values = new LinkedHashMap<>(values);
cloned = true;
} else {
try {
entry.setValue(serialized);
} catch (UnsupportedOperationException ex) {
values = new LinkedHashMap<>(values);
cloned = true;
}
}
}
if (cloned) {
values.put(entry.getKey(), serialized);
}
}
}
return values;