public class TaggedFieldSerializer<T> extends FieldSerializer<T>
@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.Tag
annotation. 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)
and
TaggedFieldSerializer.TaggedFieldSerializerConfig.setChunkedEncoding(boolean)
. Additionally, a varint is written before each field for the
tag value.
If readUnknownTagData
and chunkedEncoding
are false, fields must not be removed but the
Deprecated
annotation 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.
Modifier and Type | Class and Description |
---|---|
static interface |
TaggedFieldSerializer.Tag
Marks a field for serialization.
|
static class |
TaggedFieldSerializer.TaggedFieldSerializerConfig
Configuration for TaggedFieldSerializer instances.
|
FieldSerializer.Bind, FieldSerializer.CachedField, FieldSerializer.FieldSerializerConfig, FieldSerializer.NotNull, FieldSerializer.Optional
Constructor and Description |
---|
TaggedFieldSerializer(Kryo kryo,
java.lang.Class type) |
TaggedFieldSerializer(Kryo kryo,
java.lang.Class type,
TaggedFieldSerializer.TaggedFieldSerializerConfig config) |
Modifier and Type | Method and Description |
---|---|
TaggedFieldSerializer.TaggedFieldSerializerConfig |
getTaggedFieldSerializerConfig() |
protected void |
initializeCachedFields()
Called when
FieldSerializer.getFields() and FieldSerializer.getCopyFields() have been repopulated. |
T |
read(Kryo kryo,
Input input,
java.lang.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(java.lang.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.
|
protected void |
writeHeader(Kryo kryo,
Output output,
T object)
Can be overidden to write data needed for
FieldSerializer.create(Kryo, Input, Class) . |
copy, create, createCopy, getCopyFields, getField, getFields, getFieldSerializerConfig, getKryo, getType, log, popTypeVariables, pushTypeVariables, updateFields
getAcceptsNull, isImmutable, setAcceptsNull, setImmutable
public TaggedFieldSerializer(Kryo kryo, java.lang.Class type)
public TaggedFieldSerializer(Kryo kryo, java.lang.Class type, TaggedFieldSerializer.TaggedFieldSerializerConfig config)
protected void initializeCachedFields()
FieldSerializer
FieldSerializer.getFields()
and FieldSerializer.getCopyFields()
have been repopulated. Subclasses can override this method to
configure or remove cached fields.initializeCachedFields
in class FieldSerializer<T>
public void removeField(java.lang.String fieldName)
FieldSerializer
removeField
in class FieldSerializer<T>
public void removeField(FieldSerializer.CachedField field)
FieldSerializer
removeField
in class FieldSerializer<T>
public void write(Kryo kryo, Output output, T object)
Serializer
This method should not be called directly, instead this serializer can be passed to Kryo
write methods that accept a
serialier.
write
in class FieldSerializer<T>
object
- May be null if Serializer.getAcceptsNull()
is true.protected void writeHeader(Kryo kryo, Output output, T object)
FieldSerializer.create(Kryo, Input, Class)
. The default implementation does nothing.public T read(Kryo kryo, Input input, java.lang.Class<? extends T> type)
Serializer
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 uses Kryo
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.
read
in class FieldSerializer<T>
Serializer.getAcceptsNull()
is true.public TaggedFieldSerializer.TaggedFieldSerializerConfig getTaggedFieldSerializerConfig()
Copyright © 2019. All Rights Reserved.