diff --git a/src/com/esotericsoftware/kryo/Kryo.java b/src/com/esotericsoftware/kryo/Kryo.java index f11c32333..5485e538b 100644 --- a/src/com/esotericsoftware/kryo/Kryo.java +++ b/src/com/esotericsoftware/kryo/Kryo.java @@ -437,6 +437,13 @@ private int insertDefaultSerializer (Class type, SerializerFactory factory) { return lowest; } + /** A single point of contact for customising new serializer instances when they are registered. + * Invoked by {@link DefaultClassResolver#register(Registration)}. + */ + public Serializer adaptNewSerializer (Serializer serializer) { + return serializer; + } + /** Returns the best matching serializer for a class. This method can be overridden to implement custom logic to choose a * serializer. */ public Serializer getDefaultSerializer (Class type) { @@ -527,7 +534,7 @@ public Registration register (Class type, Serializer serializer, int id) { *

* IDs must be the same at deserialization as they were for serialization. *

- * Registration can be suclassed to efficiently store per type information, accessible in serializers via + * Registration can be subclassed to efficiently store per type information, accessible in serializers via * {@link Kryo#getRegistration(Class)}. */ public Registration register (Registration registration) { int id = registration.getId(); diff --git a/src/com/esotericsoftware/kryo/util/DefaultClassResolver.java b/src/com/esotericsoftware/kryo/util/DefaultClassResolver.java index 500e1a97f..d4cdea049 100644 --- a/src/com/esotericsoftware/kryo/util/DefaultClassResolver.java +++ b/src/com/esotericsoftware/kryo/util/DefaultClassResolver.java @@ -26,6 +26,7 @@ import com.esotericsoftware.kryo.Kryo; import com.esotericsoftware.kryo.KryoException; import com.esotericsoftware.kryo.Registration; +import com.esotericsoftware.kryo.Serializer; import com.esotericsoftware.kryo.io.Input; import com.esotericsoftware.kryo.io.Output; @@ -57,6 +58,16 @@ public Registration register (Registration registration) { memoizedClassId = -1; memoizedClass = null; if (registration == null) throw new IllegalArgumentException("registration cannot be null."); + + final Serializer oldSerializer = registration.getSerializer(); + final Serializer newSerializer = kryo.adaptNewSerializer(oldSerializer); + if (oldSerializer != newSerializer) { + if (newSerializer == null) { + throw new IllegalArgumentException("serializer cannot be null."); + } + registration.setSerializer(newSerializer); + } + if (registration.getId() != NAME) { if (TRACE) { trace("kryo", "Register class ID " + registration.getId() + ": " + className(registration.getType()) + " ("