Skip to content

Commit

Permalink
Add a hook to intercept registration of all new serializers.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisr3 committed Mar 24, 2023
1 parent 3fb0a62 commit e5a2d16
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/com/esotericsoftware/kryo/Kryo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -527,7 +534,7 @@ public Registration register (Class type, Serializer serializer, int id) {
* <p>
* IDs must be the same at deserialization as they were for serialization.
* <p>
* 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();
Expand Down
8 changes: 8 additions & 0 deletions src/com/esotericsoftware/kryo/util/DefaultClassResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -57,6 +58,13 @@ 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) {
registration.setSerializer(newSerializer);
}

if (registration.getId() != NAME) {
if (TRACE) {
trace("kryo", "Register class ID " + registration.getId() + ": " + className(registration.getType()) + " ("
Expand Down

0 comments on commit e5a2d16

Please sign in to comment.