com.esotericsoftware.kryo.serializers.EnumNameSerializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kryo Show documentation
Show all versions of kryo Show documentation
Fast, efficient Java serialization. This is the "main" kryo artifact, with a regular dependency on reflectasm.
package com.esotericsoftware.kryo.serializers;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.KryoException;
import com.esotericsoftware.kryo.Serializer;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
/** Serializes enums using the enum's name. This prevents invalidating previously serialized byts when the enum order changes.
* @author KwonNam Son */
public class EnumNameSerializer extends Serializer {
private final Class extends Enum> enumType;
private final Serializer stringSerializer;
public EnumNameSerializer (Kryo kryo, Class extends Enum> type) {
this.enumType = type;
stringSerializer = kryo.getSerializer(String.class);
setImmutable(true);
}
public void write (Kryo kryo, Output output, Enum object) {
kryo.writeObject(output, object.name(), stringSerializer);
}
public Enum read (Kryo kryo, Input input, Class type) {
String name = kryo.readObject(input, String.class, stringSerializer);
try {
return Enum.valueOf(enumType, name);
} catch (IllegalArgumentException e) {
throw new KryoException("Invalid name for enum \"" + enumType.getName() + "\": " + name, e);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy