Class FieldSerializer<T>
- java.lang.Object
-
- com.esotericsoftware.kryo.Serializer<T>
-
- com.esotericsoftware.kryo.serializers.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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
FieldSerializer.Bind
Used to annotate fields with a specific Kryo serializer.static class
FieldSerializer.CachedField
Settings for serializing a field.static class
FieldSerializer.FieldSerializerConfig
Configuration for FieldSerializer instances.static interface
FieldSerializer.NotNull
Indicates a field can never be null when it is being serialized and deserialized.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, Class type)
FieldSerializer(Kryo kryo, Class type, FieldSerializer.FieldSerializerConfig config)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description T
copy(Kryo kryo, T original)
Returns a copy of the specified object.protected T
create(Kryo kryo, Input input, Class<? extends 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 a new object.FieldSerializer.CachedField[]
getCopyFields()
Returns the fields used for copying.FieldSerializer.CachedField
getField(String fieldName)
Returns the field with the specified name, allowing field specific settings to be configured.FieldSerializer.CachedField[]
getFields()
Returns the fields used for serialization.FieldSerializer.FieldSerializerConfig
getFieldSerializerConfig()
If the returned config settings are modified,updateFields()
must be called.Kryo
getKryo()
Class
getType()
protected void
initializeCachedFields()
Called whengetFields()
andgetCopyFields()
have been repopulated.protected void
log(String prefix, FieldSerializer.CachedField cachedField, int position)
protected void
popTypeVariables(int pop)
protected int
pushTypeVariables()
Prepares the type variables for the serialized type.T
read(Kryo kryo, Input input, 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(String fieldName)
Removes a field so that it won't be serialized.void
updateFields()
Must be called aftergetFieldSerializerConfig()
settings are changed to repopulate the cached fields.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, setImmutable
-
-
-
-
Constructor Detail
-
FieldSerializer
public FieldSerializer(Kryo kryo, Class type, FieldSerializer.FieldSerializerConfig config)
-
-
Method Detail
-
initializeCachedFields
protected void initializeCachedFields()
Called whengetFields()
andgetCopyFields()
have been repopulated. Subclasses can override this method to configure or remove cached fields.
-
getFieldSerializerConfig
public FieldSerializer.FieldSerializerConfig getFieldSerializerConfig()
If the returned config settings are modified,updateFields()
must be called.
-
updateFields
public void updateFields()
Must be called aftergetFieldSerializerConfig()
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 classSerializer<T>
object
- May be null ifSerializer.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 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.
-
pushTypeVariables
protected int pushTypeVariables()
Prepares the type variables for the serialized type. Must be balanced withpopTypeVariables(int)
if >0 is returned.
-
popTypeVariables
protected void popTypeVariables(int pop)
-
create
protected T create(Kryo kryo, Input input, Class<? extends 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)
.
-
log
protected void log(String prefix, FieldSerializer.CachedField cachedField, int position)
-
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.
-
removeField
public void removeField(FieldSerializer.CachedField field)
Removes a field so that it won't be serialized.
-
getFields
public FieldSerializer.CachedField[] getFields()
Returns the fields used for serialization.
-
getCopyFields
public FieldSerializer.CachedField[] getCopyFields()
Returns the fields used for copying.
-
getType
public Class getType()
-
getKryo
public Kryo getKryo()
-
createCopy
protected T createCopy(Kryo kryo, T original)
Used bycopy(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 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 can optionall override this method 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. A 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>
-
-