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 25, 2023
1 parent 3fb0a62 commit ef968b5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/com/esotericsoftware/kryo/Kryo.java
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
11 changes: 11 additions & 0 deletions src/com/esotericsoftware/kryo/util/DefaultClassResolver.java
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,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()) + " ("
Expand Down

0 comments on commit ef968b5

Please sign in to comment.