spring-projects / spring-statemachine

Spring Statemachine is a framework for application developers to use state machine concepts with Spring.
1.53k stars 599 forks source link

Add the possibility to register a custom Kryo serializer #1024

Open anakiou opened 2 years ago

anakiou commented 2 years ago

Hello team, Thank you very much for your efforts, much appreciated. I believe it would be useful if there was a way to register a custom Kryo serializer. The use case is case when using Enums for the State or Events. The default Kryo serializer for Enums does the following:

public void write (Kryo kryo, Output output, Enum object) {
            if (object == null) {
                output.writeVarInt(NULL, true);
                return;
            }
            output.writeVarInt(object.ordinal() + 1, true);
        }

        public Enum read (Kryo kryo, Input input, Class<? extends Enum> type) {
            int ordinal = input.readVarInt(true);
            if (ordinal == NULL) return null;
            ordinal--;
            if (ordinal < 0 || ordinal > enumConstants.length - 1)
                throw new KryoException("Invalid ordinal for enum \"" + type.getName() + "\": " + ordinal);
            Object constant = enumConstants[ordinal];
            return (Enum)constant;
        }

This means that if you modify the Enum (adding or removing states), best case it would throw a KryoException and would not be able to deserialize, worst case the states and event will be mixed up. Kryo provides a serializer for Enums using the name but there is no way at the moment to access the Kryo instance and configure it. A good option would be to be able to register a custom Kryo serializer from the configuration.

Thanks,

davidpelayo commented 1 year ago

Any idea of when this will be triaged?