Class 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.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.

    Author:
    Nathan Sweet