Class FieldSerializer<T>
- java.lang.Object
-
- com.esotericsoftware.kryo.Serializer<T>
-
- com.esotericsoftware.kryo.serializers.FieldSerializer<T>
-
- All Implemented Interfaces:
java.util.Comparator<FieldSerializer.CachedField>
- Direct Known Subclasses:
CompatibleFieldSerializer
,TaggedFieldSerializer
public class FieldSerializer<T> extends Serializer<T> implements java.util.Comparator<FieldSerializer.CachedField>
Serializes objects using direct field assignment. No header or schema data is stored, only the data for each field. This reduces output size but means if any field is added or removed, previously serialized bytes are invalidated. If fields are public, bytecode generation will be used instead of reflection.- See Also:
Serializer
,Kryo.register(Class, Serializer)
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description class
FieldSerializer.CachedField<X>
Controls how a field will be serialized.static interface
FieldSerializer.Optional
Indicates a field should be ignored when its declaring class is registered unless thecontext
has a value set for the specified key.
-
Constructor Summary
Constructors Constructor Description FieldSerializer(Kryo kryo, java.lang.Class type)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
compare(FieldSerializer.CachedField o1, FieldSerializer.CachedField o2)
T
copy(Kryo kryo, T original)
Returns a copy of the specified object.protected T
create(Kryo kryo, Input input, java.lang.Class<T> type)
Used byread(Kryo, Input, Class)
to create the new object.protected T
createCopy(Kryo kryo, T original)
Used bycopy(Kryo, Object)
to create the new object.FieldSerializer.CachedField
getField(java.lang.String fieldName)
Allows specific fields to be optimized.FieldSerializer.CachedField[]
getFields()
java.lang.Class
getType()
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(java.lang.String fieldName)
Removes a field so that it won't be serialized.void
setFieldsAsAccessible(boolean setFieldsAsAccessible)
Controls which fields are serialized.void
setFieldsCanBeNull(boolean fieldsCanBeNull)
Sets the default value forFieldSerializer.CachedField.setCanBeNull(boolean)
.void
setFixedFieldTypes(boolean fixedFieldTypes)
Sets the default value forFieldSerializer.CachedField.setClass(Class)
to the field's declared type.void
setIgnoreSyntheticFields(boolean ignoreSyntheticFields)
Controls if synthetic fields are serialized.void
write(Kryo kryo, Output output, T object)
Writes the bytes for the object to the output.-
Methods inherited from class com.esotericsoftware.kryo.Serializer
getAcceptsNull, isImmutable, setAcceptsNull, setGenerics, setImmutable
-
-
-
-
Constructor Detail
-
FieldSerializer
public FieldSerializer(Kryo kryo, java.lang.Class type)
-
-
Method Detail
-
initializeCachedFields
protected void initializeCachedFields()
-
compare
public int compare(FieldSerializer.CachedField o1, FieldSerializer.CachedField o2)
- Specified by:
compare
in interfacejava.util.Comparator<T>
-
setFieldsCanBeNull
public void setFieldsCanBeNull(boolean fieldsCanBeNull)
Sets the default value forFieldSerializer.CachedField.setCanBeNull(boolean)
. Calling this method resets thecached fields
.- Parameters:
fieldsCanBeNull
- False if none of the fields are null. Saves 0-1 byte per field. True if it is not known (default).
-
setFieldsAsAccessible
public void setFieldsAsAccessible(boolean setFieldsAsAccessible)
Controls which fields are serialized. Calling this method resets thecached fields
.- Parameters:
setFieldsAsAccessible
- If true, all non-transient fields (inlcuding private fields) will be serialized andset as accessible
if necessary (default). If false, only fields in the public API will be serialized.
-
setIgnoreSyntheticFields
public void setIgnoreSyntheticFields(boolean ignoreSyntheticFields)
Controls if synthetic fields are serialized. Default is true. Calling this method resets thecached fields
.- Parameters:
ignoreSyntheticFields
- If true, only non-synthetic fields will be serialized.
-
setFixedFieldTypes
public void setFixedFieldTypes(boolean fixedFieldTypes)
Sets the default value forFieldSerializer.CachedField.setClass(Class)
to the field's declared type. This allows FieldSerializer to be more efficient, since it knows field values will not be a subclass of their declared type. Default is false. Calling this method resets thecached 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 classSerializer<T>
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.- Specified by:
read
in classSerializer<T>
- Returns:
- May be null if
Serializer.getAcceptsNull()
is true.
-
create
protected T create(Kryo kryo, Input input, java.lang.Class<T> type)
Used byread(Kryo, Input, Class)
to create the new object. This can be overridden to customize object creation, eg to call a constructor with arguments. The default implementation usesKryo.newInstance(Class)
.
-
getField
public FieldSerializer.CachedField getField(java.lang.String fieldName)
Allows specific fields to be optimized.
-
removeField
public void removeField(java.lang.String fieldName)
Removes a field so that it won't be serialized.
-
getFields
public FieldSerializer.CachedField[] getFields()
-
getType
public java.lang.Class getType()
-
createCopy
protected T createCopy(Kryo kryo, T original)
Used bycopy(Kryo, Object)
to create the new object. This can be overridden to customize object creation, eg to call a constructor with arguments. The default implementation usesKryo.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 ifSerializer.isImmutable()
is true, else throwsKryoException
. Subclasses should override this method if needed to supportKryo.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. Any serializer that usesKryo
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 classSerializer<T>
-
-