All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.esotericsoftware.kryo.serializers.EnumNameSerializer Maven / Gradle / Ivy

The newest version!

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 enumType;
	private final Serializer stringSerializer;

	public EnumNameSerializer (Kryo kryo, Class 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