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 interfaceFieldSerializer.BindUsed to annotate fields with a specific Kryo serializer.static classFieldSerializer.CachedFieldSettings for serializing a field.static classFieldSerializer.FieldSerializerConfigConfiguration for FieldSerializer instances.static interfaceFieldSerializer.NotNullIndicates a field can never be null when it is being serialized and deserialized.static interfaceFieldSerializer.OptionalIndicates a field should be ignored when its declaring class is registered unless thecontexthas 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 Tcopy(Kryo kryo, T original)Returns a copy of the specified object.protected Tcreate(Kryo kryo, Input input, Class<? extends T> type)Used byread(Kryo, Input, Class)to create the new object.protected TcreateCopy(Kryo kryo, T original)Used bycopy(Kryo, Object)to create a new object.FieldSerializer.CachedField[]getCopyFields()Returns the fields used for copying.FieldSerializer.CachedFieldgetField(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.FieldSerializerConfiggetFieldSerializerConfig()If the returned config settings are modified,updateFields()must be called.KryogetKryo()ClassgetType()protected voidinitializeCachedFields()Called whengetFields()andgetCopyFields()have been repopulated.protected voidlog(String prefix, FieldSerializer.CachedField cachedField, int position)protected voidpopTypeVariables(int pop)protected intpushTypeVariables()Prepares the type variables for the serialized type.Tread(Kryo kryo, Input input, Class<? extends T> type)Reads bytes and returns a new object of the specified concrete type.voidremoveField(FieldSerializer.CachedField field)Removes a field so that it won't be serialized.voidremoveField(String fieldName)Removes a field so that it won't be serialized.voidupdateFields()Must be called aftergetFieldSerializerConfig()settings are changed to repopulate the cached fields.voidwrite(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:SerializerWrites the bytes for the object to the output.This method should not be called directly, instead this serializer can be passed to
Kryowrite methods that accept a serialier.- Specified by:
writein 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:SerializerReads 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 usesKryoto read a child object may need to be reentrant.This method should not be called directly, instead this serializer can be passed to
Kryoread methods that accept a serialier.- Specified by:
readin 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:SerializerReturns 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 usesKryoto copy a child object may need to be reentrant.This method should not be called directly, instead this serializer can be passed to
Kryocopy methods that accept a serialier.- Overrides:
copyin classSerializer<T>
-
-