Class FieldSerializer<T>

  • Direct Known Subclasses:
    CompatibleFieldSerializer, TaggedFieldSerializer, VersionFieldSerializer

    public class FieldSerializer<T>
    extends Serializer<T>
    Serializes objects using direct field assignment. FieldSerializer is generic and can serialize most classes without any configuration. All non-public fields are written and read by default, so it is important to evaluate each class that will be serialized. If fields are public, serialization may be faster.

    FieldSerializer is efficient by writing only the field data, without any schema information, using the Java class files as the schema. It does not support adding, removing, or changing the type of fields without invalidating previously serialized bytes. Renaming fields is allowed only if it doesn't change the alphabetical order of the fields.

    FieldSerializer's compatibility drawbacks can be acceptable in many situations, such as when sending data over a network, but may not be a good choice for long term data storage because the Java classes cannot evolve. Subclasses provided more flexible compatibility.

    Author:
    Nathan Sweet, Roman Levenstein
    See Also:
    Serializer, Kryo.register(Class, Serializer), VersionFieldSerializer, TaggedFieldSerializer, CompatibleFieldSerializer
    • Method Detail

      • initializeCachedFields

        protected void initializeCachedFields()
        Called when getFields() and getCopyFields() have been repopulated. Subclasses can override this method to configure or remove cached fields.
      • updateFields

        public void updateFields()
        Must be called after getFieldSerializerConfig() settings are changed to repopulate the cached fields.
      • write

        public void write​(Kryo kryo,
                          Output output,
                          T object)
        Description copied from class: Serializer
        Writes the bytes for the object to the output.

        This method should not be called directly, instead this serializer can be passed to Kryo write methods that accept a serialier.

        Specified by:
        write in class Serializer<T>
        object - May be null if Serializer.getAcceptsNull() is true.
      • read

        public T read​(Kryo kryo,
                      Input input,
                      Class<? extends 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 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.

        Specified by:
        read in class Serializer<T>
        Returns:
        May be null if Serializer.getAcceptsNull() is true.
      • pushTypeVariables

        protected int pushTypeVariables()
        Prepares the type variables for the serialized type. Must be balanced with popTypeVariables(int) if >0 is returned.
      • popTypeVariables

        protected void popTypeVariables​(int pop)
      • getField

        public FieldSerializer.CachedField getField​(String fieldName)
        Returns the field with the specified name, allowing field specific settings to be configured.
      • removeField

        public void removeField​(String fieldName)
        Removes a field so that it won't be serialized.
      • getType

        public Class getType()
      • getKryo

        public Kryo getKryo()
      • createCopy

        protected T createCopy​(Kryo kryo,
                               T original)
        Used by copy(Kryo, Object) to create a new object. This can be overridden to customize object creation, eg to call a constructor with arguments. The default implementation uses Kryo.newInstance(Class).
      • copy

        public T copy​(Kryo kryo,
                      T original)
        Description copied from class: Serializer
        Returns a copy of the specified object. The default implementation returns the original if Serializer.isImmutable() is true, else throws KryoException. Subclasses can optionall override this method to support Kryo.copy(Object).

        Before Kryo can be used to copy child objects, Kryo.reference(Object) must be called with the copy to ensure it can be referenced by the child objects. A serializer that uses Kryo to copy a child object may need to be reentrant.

        This method should not be called directly, instead this serializer can be passed to Kryo copy methods that accept a serialier.

        Overrides:
        copy in class Serializer<T>