serializationenumskryo

Kryo Deserialzation Issue : Invalid ordinal value for Enum


I have been working on Kryo to serialize my data and save it in a database. Later I try to fetch the serialized data again and try to deserialize it. Although most of the data that, I serialize get deserialized correctly, there are some scenarios where I get the following exception Trace for some data:

com.esotericsoftware.kryo.KryoException: Invalid ordinal for enum "test.swift.mt564.Code4Ec335Type": 29
Serialization trace:
dataMap (parser.container.TestDataCommon)
        at com.esotericsoftware.kryo.serializers.DefaultSerializers$EnumSerializer.read(DefaultSerializers.java:331)
        at com.esotericsoftware.kryo.serializers.DefaultSerializers$EnumSerializer.read(DefaultSerializers.java:305)
        at com.esotericsoftware.kryo.Kryo.readClassAndObject(Kryo.java:767)
        at com.esotericsoftware.kryo.serializers.MapSerializer.read(MapSerializer.java:139)
        at com.esotericsoftware.kryo.serializers.MapSerializer.read(MapSerializer.java:17)
        at com.esotericsoftware.kryo.Kryo.readObject(Kryo.java:685)
        at com.esotericsoftware.kryo.serializers.ObjectField.read(ObjectField.java:106)
        at com.esotericsoftware.kryo.serializers.FieldSerializer.read(FieldSerializer.java:482)
        at com.esotericsoftware.kryo.Kryo.readClassAndObject(Kryo.java:767)
        at java.lang.Thread.run(Thread.java:745)

This data was serialized and saved in the database as a BLOB a few months back. Now I am finding a need to retrieve this data and deserialize it. The above exception occurs when I try to desiralize the data into the required object.

I know that while deserializing Enum Kryo uses ordinal value of Enum. But due to some reason it gets very high value of 30 for ordinal. I am not sure what is actually causing this problem.


Solution

  • Above issue is due to the way in which the classes are registered with Kryo. I think, you have used kryo.register(Class) method for registering the class.

    There has been some new class which has been added to the kryo registration list after the value was serialized few months back.

    You can verify by removing the newly added class from registration to test the flow.

    Also to avoid this issue when new classes are added in future provide an explicit id to the classes which are being registered using kryo.register(Class, int) method.