Skip to content

Commit e5a2d16

Browse files
committed
Add a hook to intercept registration of all new serializers.
1 parent 3fb0a62 commit e5a2d16

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/com/esotericsoftware/kryo/Kryo.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,13 @@ private int insertDefaultSerializer (Class type, SerializerFactory factory) {
437437
return lowest;
438438
}
439439

440+
/** A single point of contact for customising new serializer instances when they are registered.
441+
* Invoked by {@link DefaultClassResolver#register(Registration)}.
442+
*/
443+
public Serializer adaptNewSerializer (Serializer serializer) {
444+
return serializer;
445+
}
446+
440447
/** Returns the best matching serializer for a class. This method can be overridden to implement custom logic to choose a
441448
* serializer. */
442449
public Serializer getDefaultSerializer (Class type) {
@@ -527,7 +534,7 @@ public Registration register (Class type, Serializer serializer, int id) {
527534
* <p>
528535
* IDs must be the same at deserialization as they were for serialization.
529536
* <p>
530-
* Registration can be suclassed to efficiently store per type information, accessible in serializers via
537+
* Registration can be subclassed to efficiently store per type information, accessible in serializers via
531538
* {@link Kryo#getRegistration(Class)}. */
532539
public Registration register (Registration registration) {
533540
int id = registration.getId();

src/com/esotericsoftware/kryo/util/DefaultClassResolver.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.esotericsoftware.kryo.Kryo;
2727
import com.esotericsoftware.kryo.KryoException;
2828
import com.esotericsoftware.kryo.Registration;
29+
import com.esotericsoftware.kryo.Serializer;
2930
import com.esotericsoftware.kryo.io.Input;
3031
import com.esotericsoftware.kryo.io.Output;
3132

@@ -57,6 +58,13 @@ public Registration register (Registration registration) {
5758
memoizedClassId = -1;
5859
memoizedClass = null;
5960
if (registration == null) throw new IllegalArgumentException("registration cannot be null.");
61+
62+
final Serializer oldSerializer = registration.getSerializer();
63+
final Serializer newSerializer = kryo.adaptNewSerializer(oldSerializer);
64+
if (oldSerializer != newSerializer) {
65+
registration.setSerializer(newSerializer);
66+
}
67+
6068
if (registration.getId() != NAME) {
6169
if (TRACE) {
6270
trace("kryo", "Register class ID " + registration.getId() + ": " + className(registration.getType()) + " ("

0 commit comments

Comments
 (0)