Class VersionFieldSerializer<T>
- java.lang.Object
-
- com.esotericsoftware.kryo.Serializer<T>
-
- com.esotericsoftware.kryo.serializers.FieldSerializer<T,FieldSerializerConfig>
-
- com.esotericsoftware.kryo.serializers.VersionFieldSerializer<T>
-
- All Implemented Interfaces:
Comparator<FieldSerializer.CachedField>
public class VersionFieldSerializer<T> extends FieldSerializer<T,FieldSerializerConfig>
Serializes objects using direct field assignment, with versioning backward compatibility. Allows fields to have a@Since(int)
annotation to indicate the version they were added. For a particular field, the value in@Since
should never change once created. This is less flexible than FieldSerializer, which can handle most classes without needing annotations, but it provides backward compatibility. This means that new fields can be added, but removing, renaming or changing the type of any field will invalidate previous serialized bytes. VersionFieldSerializer has very little overhead (a single additional varint) compared to FieldSerializer. Forward compatibility is not supported.- Author:
- Tianyi HE
- See Also:
TaggedFieldSerializer
-
-
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.-
Nested classes/interfaces inherited from class com.esotericsoftware.kryo.serializers.FieldSerializer
FieldSerializer.Bind, FieldSerializer.CachedField<X>, FieldSerializer.CachedFieldFactory, FieldSerializer.CachedFieldNameStrategy, FieldSerializer.Optional
-
-
Field Summary
-
Fields inherited from class com.esotericsoftware.kryo.serializers.FieldSerializer
config, removedFields
-
-
Constructor Summary
Constructors Constructor Description VersionFieldSerializer(Kryo kryo, Class type)
VersionFieldSerializer(Kryo kryo, Class type, boolean compatible)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
initializeCachedFields()
T
read(Kryo kryo, Input input, Class<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)
This method can be called for different fields having the same type.-
Methods inherited from class com.esotericsoftware.kryo.serializers.FieldSerializer
compare, copy, create, createCopy, getCachedFieldName, getConfig, getCopyTransient, getField, getFields, getGenerics, getKryo, getSerializeTransient, getTransientFields, getType, rebuildCachedFields, rebuildCachedFields, setCopyTransient, setFieldsAsAccessible, setFieldsCanBeNull, setFixedFieldTypes, setGenerics, setIgnoreSyntheticFields, setOptimizedGenerics, setSerializeTransient
-
Methods inherited from class com.esotericsoftware.kryo.Serializer
getAcceptsNull, isImmutable, setAcceptsNull, setImmutable
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.util.Comparator
equals, reversed, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong
-
-
-
-
Method Detail
-
initializeCachedFields
protected void initializeCachedFields()
- Overrides:
initializeCachedFields
in classFieldSerializer<T,FieldSerializerConfig>
-
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,FieldSerializerConfig>
-
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,FieldSerializerConfig>
-
write
public void write(Kryo kryo, Output output, T object)
Description copied from class:FieldSerializer
This method can be called for different fields having the same type. Even though the raw type is the same, if the type is generic, it could happen that different concrete classes are used to instantiate it. Therefore, in case of different instantiation parameters, the fields analysis should be repeated. TODO: Cache serializer instances generated for a given set of generic parameters. Reuse it later instead of recomputing every time.- Overrides:
write
in classFieldSerializer<T,FieldSerializerConfig>
object
- May be null ifSerializer.getAcceptsNull()
is true.
-
read
public T read(Kryo kryo, Input input, Class<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,FieldSerializerConfig>
- Returns:
- May be null if
Serializer.getAcceptsNull()
is true.
-
-