Class TaggedFieldSerializer<T>
- java.lang.Object
-
- com.esotericsoftware.kryo.Serializer<T>
-
- com.esotericsoftware.kryo.serializers.FieldSerializer<T>
-
- com.esotericsoftware.kryo.serializers.TaggedFieldSerializer<T>
-
public class TaggedFieldSerializer<T> extends FieldSerializer<T>
Serializes objects using direct field assignment for fields that have a@Tag(int)annotation, providing backward compatibility and optional forward compatibility. This means fields can be added or renamed and optionally removed without invalidating previously serialized bytes. Changing the type of a field is not supported.Fields are identified by the
TaggedFieldSerializer.Tagannotation. Fields can be renamed without affecting serialization. Field tag values must be unique, both within a class and all its super classes. An exception is thrown if duplicate tag values are encountered.The forward and backward compatibility and serialization performance depend on
TaggedFieldSerializer.TaggedFieldSerializerConfig.setReadUnknownTagData(boolean)andTaggedFieldSerializer.TaggedFieldSerializerConfig.setChunkedEncoding(boolean). Additionally, a varint is written before each field for the tag value.If
readUnknownTagDataandchunkedEncodingare false, fields must not be removed but theDeprecatedannotation can be applied. Deprecated fields are read when reading old bytes but aren't written to new bytes. Classes can evolve by reading the values of deprecated fields and writing them elsewhere. Fields can be renamed and/or made private to reduce clutter in the class (eg,ignored1,ignored2).Compared to
VersionFieldSerializer, TaggedFieldSerializer allows renaming and deprecating fields, so has more flexibility for classes to evolve. This comes at the cost of one varint per field.- Author:
- Nathan Sweet
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceTaggedFieldSerializer.TagMarks a field for serialization.static classTaggedFieldSerializer.TaggedFieldSerializerConfigConfiguration for TaggedFieldSerializer 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 TaggedFieldSerializer(Kryo kryo, java.lang.Class type)TaggedFieldSerializer(Kryo kryo, java.lang.Class type, TaggedFieldSerializer.TaggedFieldSerializerConfig config)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description TaggedFieldSerializer.TaggedFieldSerializerConfiggetTaggedFieldSerializerConfig()protected voidinitializeCachedFields()Called whenFieldSerializer.getFields()andFieldSerializer.getCopyFields()have been repopulated.Tread(Kryo kryo, Input input, java.lang.Class<? extends T> type)Reads bytes and returns a new object of the specified concrete type.voidremoveField(FieldSerializer.CachedField field)Removes a field so that it won't be serialized.voidremoveField(java.lang.String fieldName)Removes a field so that it won't be serialized.voidwrite(Kryo kryo, Output output, T object)Writes the bytes for the object to the output.protected voidwriteHeader(Kryo kryo, Output output, T object)Can be overidden to write data needed forFieldSerializer.create(Kryo, Input, Class).-
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
-
TaggedFieldSerializer
public TaggedFieldSerializer(Kryo kryo, java.lang.Class type)
-
TaggedFieldSerializer
public TaggedFieldSerializer(Kryo kryo, java.lang.Class type, TaggedFieldSerializer.TaggedFieldSerializerConfig config)
-
-
Method Detail
-
initializeCachedFields
protected void initializeCachedFields()
Description copied from class:FieldSerializerCalled whenFieldSerializer.getFields()andFieldSerializer.getCopyFields()have been repopulated. Subclasses can override this method to configure or remove cached fields.- Overrides:
initializeCachedFieldsin classFieldSerializer<T>
-
removeField
public void removeField(java.lang.String fieldName)
Description copied from class:FieldSerializerRemoves a field so that it won't be serialized.- Overrides:
removeFieldin classFieldSerializer<T>
-
removeField
public void removeField(FieldSerializer.CachedField field)
Description copied from class:FieldSerializerRemoves a field so that it won't be serialized.- Overrides:
removeFieldin classFieldSerializer<T>
-
write
public void write(Kryo kryo, Output output, T object)
Description copied from class:SerializerWrites the bytes for the object to the output.This method should not be called directly, instead this serializer can be passed to
Kryowrite methods that accept a serialier.- Overrides:
writein classFieldSerializer<T>object- May be null ifSerializer.getAcceptsNull()is true.
-
writeHeader
protected void writeHeader(Kryo kryo, Output output, T object)
Can be overidden to write data needed forFieldSerializer.create(Kryo, Input, Class). The default implementation does nothing.
-
read
public T read(Kryo kryo, Input input, java.lang.Class<? extends T> type)
Description copied from class:SerializerReads 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 usesKryoto read a child object may need to be reentrant.This method should not be called directly, instead this serializer can be passed to
Kryoread methods that accept a serialier.- Overrides:
readin classFieldSerializer<T>- Returns:
- May be null if
Serializer.getAcceptsNull()is true.
-
getTaggedFieldSerializerConfig
public TaggedFieldSerializer.TaggedFieldSerializerConfig getTaggedFieldSerializerConfig()
-
-