Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a hook to intercept registration of all new serializers. #947

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
11 changes: 11 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,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