Class VersionFieldSerializer<T>
- java.lang.Object
-
- com.esotericsoftware.kryo.Serializer<T>
-
- com.esotericsoftware.kryo.serializers.FieldSerializer<T>
-
- com.esotericsoftware.kryo.serializers.VersionFieldSerializer<T>
-
public class VersionFieldSerializer<T> extends FieldSerializer<T>
Serializes objects using direct field assignment, providing backward compatibility with minimal overhead. This means fields can be added without invalidating previously serialized bytes. Removing, renaming, or changing the type of a field is not supported.When a field is added, it must have the
VersionFieldSerializer.Since
annotation to indicate the version it was added in order to be compatible with previously serialized bytes. The annotation value must never change.Compared to
FieldSerializer
, VersionFieldSerializer writes a single additional varint and requires annotations for added fields, but provides backward compatibility so fields can be added.TaggedFieldSerializer
provides more flexibility for classes to evolve in exchange for a slightly larger serialized size.- Author:
- Nathan Sweet
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
VersionFieldSerializer.Since
Incremental modification of serialized objects must addVersionFieldSerializer.Since
for new fields.static class
VersionFieldSerializer.VersionFieldSerializerConfig
Configuration for VersionFieldSerializer instances.-
Nested classes/interfaces inherited from class com.esotericsoftware.kryo.serializers.FieldSerializer
FieldSerializer.Bind, FieldSerializer.CachedField, FieldSerializer.FieldSerializerConfig, FieldSerializer.NotNull, FieldSerializer.Optional
-
-
Constructor Summary
Constructors Constructor Description VersionFieldSerializer(Kryo kryo, Class type)
VersionFieldSerializer(Kryo kryo, Class type, VersionFieldSerializer.VersionFieldSerializerConfig config)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description VersionFieldSerializer.VersionFieldSerializerConfig
getVersionFieldSerializerConfig()
protected void
initializeCachedFields()
Called whenFieldSerializer.getFields()
andFieldSerializer.getCopyFields()
have been repopulated.T
read(Kryo kryo, Input input, Class<? extends T> type)
Reads bytes and returns a new object of the specified concrete type.void
removeField(FieldSerializer.CachedField field)
Removes a field so that it won't be serialized.void
removeField(String fieldName)
Removes a field so that it won't be serialized.void
write(Kryo kryo, Output output, T object)
Writes the bytes for the object to the output.-
Methods inherited from class com.esotericsoftware.kryo.serializers.FieldSerializer
copy, create, createCopy, getCopyFields, getField, getFields, getFieldSerializerConfig, getKryo, getType, log, popTypeVariables, pushTypeVariables, updateFields
-
Methods inherited from class com.esotericsoftware.kryo.Serializer
getAcceptsNull, isImmutable, setAcceptsNull, setImmutable
-
-
-
-
Constructor Detail
-
VersionFieldSerializer
public VersionFieldSerializer(Kryo kryo, Class type, VersionFieldSerializer.VersionFieldSerializerConfig config)
-
-
Method Detail
-
initializeCachedFields
protected void initializeCachedFields()
Description copied from class:FieldSerializer
Called whenFieldSerializer.getFields()
andFieldSerializer.getCopyFields()
have been repopulated. Subclasses can override this method to configure or remove cached fields.- Overrides:
initializeCachedFields
in classFieldSerializer<T>
-
removeField
public void removeField(String fieldName)
Description copied from class:FieldSerializer
Removes a field so that it won't be serialized.- Overrides:
removeField
in classFieldSerializer<T>
-
removeField
public void removeField(FieldSerializer.CachedField field)
Description copied from class:FieldSerializer
Removes a field so that it won't be serialized.- Overrides:
removeField
in classFieldSerializer<T>
-
write
public void write(Kryo kryo, Output output, T object)
Description copied from class:Serializer
Writes the bytes for the object to the output.This method should not be called directly, instead this serializer can be passed to
Kryo
write methods that accept a serialier.- Overrides:
write
in classFieldSerializer<T>
object
- May be null ifSerializer.getAcceptsNull()
is true.
-
read
public T read(Kryo kryo, Input input, Class<? extends T> type)
Description copied from class:Serializer
Reads bytes and returns a new object of the specified concrete type.Before Kryo can be used to read child objects,
Kryo.reference(Object)
must be called with the parent object to ensure it can be referenced by the child objects. Any serializer that usesKryo
to read a child object may need to be reentrant.This method should not be called directly, instead this serializer can be passed to
Kryo
read methods that accept a serialier.- Overrides:
read
in classFieldSerializer<T>
- Returns:
- May be null if
Serializer.getAcceptsNull()
is true.
-
getVersionFieldSerializerConfig
public VersionFieldSerializer.VersionFieldSerializerConfig getVersionFieldSerializerConfig()
-
-