Class TaggedFieldSerializer<T>
- java.lang.Object
-
- com.esotericsoftware.kryo.Serializer<T>
-
- com.esotericsoftware.kryo.serializers.FieldSerializer<T,TaggedFieldSerializerConfig>
-
- com.esotericsoftware.kryo.serializers.TaggedFieldSerializer<T>
-
- All Implemented Interfaces:
java.util.Comparator<FieldSerializer.CachedField>
public class TaggedFieldSerializer<T> extends FieldSerializer<T,TaggedFieldSerializerConfig>
Serializes objects using direct field assignment for fields that have a@Tag(int)
annotation. This provides backward compatibility so new fields can be added. TaggedFieldSerializer has two advantages overVersionFieldSerializer
: 1) fields can be renamed and 2) fields marked with the@Deprecated
annotation will be ignored when reading old bytes and won't be written to new bytes. Deprecation effectively removes the field from serialization, though the field and@Tag
annotation must remain in the class. Deprecated fields can optionally be made private and/or renamed so they don't clutter the class (eg,ignored
,ignored2
). For these reasons, TaggedFieldSerializer generally provides more flexibility for classes to evolve. The downside is that it has a small amount of additional overhead compared to VersionFieldSerializer (an additional varint per field).Forward compatibility is optionally supported by enabling
TaggedFieldSerializerConfig.setSkipUnknownTags(boolean)
, which allows it to skip reading unknown tagged fields, which are presumably new fields added in future versions of an application. The data is only forward compatible if the newly added fields are tagged withTaggedFieldSerializer.Tag.annexed()
set true, which comes with the cost of chunked encoding. When annexed fields are encountered during the read or write process of an object, a buffer is allocated to perform the chunked encoding.Tag values must be entirely unique, even among a class and its superclass(es). An IllegalArgumentException will be thrown by
Kryo.register(Class)
(and its overloads) if duplicate Tag values are encountered.- See Also:
VersionFieldSerializer
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
TaggedFieldSerializer.Tag
Marks a field for serialization.-
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 Modifier and Type Field Description private boolean[]
annexed
private boolean[]
deprecated
private static java.util.Comparator<FieldSerializer.CachedField>
TAGGED_VALUE_COMPARATOR
private int[]
tags
private int
writeFieldCount
-
Fields inherited from class com.esotericsoftware.kryo.serializers.FieldSerializer
access, asmFieldFactory, componentType, config, kryo, objectFieldFactory, removedFields, type, typeParameters
-
-
Constructor Summary
Constructors Constructor Description TaggedFieldSerializer(Kryo kryo, java.lang.Class type)
TaggedFieldSerializer(Kryo kryo, java.lang.Class type, TaggedFieldSerializerConfig config)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
initializeCachedFields()
T
read(Kryo kryo, Input input, java.lang.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(java.lang.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, getGenericsScope, getKryo, getSerializeTransient, getTransientFields, getType, newCachedField, newMatchingCachedField, rebuildCachedFields, rebuildCachedFields, setCopyTransient, setFieldsAsAccessible, setFieldsCanBeNull, setFixedFieldTypes, setGenerics, setIgnoreSyntheticFields, setOptimizedGenerics, setSerializeTransient
-
Methods inherited from class com.esotericsoftware.kryo.Serializer
getAcceptsNull, isImmutable, setAcceptsNull, setImmutable
-
-
-
-
Field Detail
-
tags
private int[] tags
-
writeFieldCount
private int writeFieldCount
-
deprecated
private boolean[] deprecated
-
annexed
private boolean[] annexed
-
TAGGED_VALUE_COMPARATOR
private static final java.util.Comparator<FieldSerializer.CachedField> TAGGED_VALUE_COMPARATOR
-
-
Constructor Detail
-
TaggedFieldSerializer
public TaggedFieldSerializer(Kryo kryo, java.lang.Class type)
-
TaggedFieldSerializer
public TaggedFieldSerializer(Kryo kryo, java.lang.Class type, TaggedFieldSerializerConfig config)
-
-
Method Detail
-
initializeCachedFields
protected void initializeCachedFields()
- Overrides:
initializeCachedFields
in classFieldSerializer<T,TaggedFieldSerializerConfig>
-
removeField
public void removeField(java.lang.String fieldName)
Description copied from class:FieldSerializer
Removes a field so that it won't be serialized.- Overrides:
removeField
in classFieldSerializer<T,TaggedFieldSerializerConfig>
-
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,TaggedFieldSerializerConfig>
-
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,TaggedFieldSerializerConfig>
object
- May be null ifSerializer.getAcceptsNull()
is true.
-
read
public T read(Kryo kryo, Input input, java.lang.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,TaggedFieldSerializerConfig>
- Returns:
- May be null if
Serializer.getAcceptsNull()
is true.
-
-