public class FieldSerializer<T> extends Serializer<T>
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.
Serializer
,
Kryo.register(Class, Serializer)
,
VersionFieldSerializer
,
TaggedFieldSerializer
,
CompatibleFieldSerializer
Modifier and Type | Class and 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 the
context has
a value set for the specified key. |
Constructor and Description |
---|
FieldSerializer(Kryo kryo,
java.lang.Class type) |
FieldSerializer(Kryo kryo,
java.lang.Class type,
FieldSerializer.FieldSerializerConfig config) |
Modifier and Type | Method and Description |
---|---|
T |
copy(Kryo kryo,
T original)
Returns a copy of the specified object.
|
protected T |
create(Kryo kryo,
Input input,
java.lang.Class<? extends T> type)
Used by
read(Kryo, Input, Class) to create the new object. |
protected T |
createCopy(Kryo kryo,
T original)
Used by
copy(Kryo, Object) to create a new object. |
FieldSerializer.CachedField[] |
getCopyFields()
Returns the fields used for copying.
|
FieldSerializer.CachedField |
getField(java.lang.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() |
java.lang.Class |
getType() |
protected void |
initializeCachedFields()
Called when
getFields() and getCopyFields() have been repopulated. |
protected void |
log(java.lang.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,
java.lang.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(java.lang.String fieldName)
Removes a field so that it won't be serialized.
|
void |
updateFields()
Must be called after
getFieldSerializerConfig() 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.
|
getAcceptsNull, isImmutable, setAcceptsNull, setImmutable
public FieldSerializer(Kryo kryo, java.lang.Class type)
public FieldSerializer(Kryo kryo, java.lang.Class type, FieldSerializer.FieldSerializerConfig config)
protected void initializeCachedFields()
getFields()
and getCopyFields()
have been repopulated. Subclasses can override this method to
configure or remove cached fields.public FieldSerializer.FieldSerializerConfig getFieldSerializerConfig()
updateFields()
must be called.public void updateFields()
getFieldSerializerConfig()
settings are changed to repopulate the cached fields.public void write(Kryo kryo, Output output, T object)
Serializer
This method should not be called directly, instead this serializer can be passed to Kryo
write methods that accept a
serialier.
write
in class Serializer<T>
object
- May be null if Serializer.getAcceptsNull()
is true.public T read(Kryo kryo, Input input, java.lang.Class<? extends T> type)
Serializer
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.
read
in class Serializer<T>
Serializer.getAcceptsNull()
is true.protected int pushTypeVariables()
popTypeVariables(int)
if >0 is
returned.protected void popTypeVariables(int pop)
protected T create(Kryo kryo, Input input, java.lang.Class<? extends T> type)
read(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 uses Kryo.newInstance(Class)
.protected void log(java.lang.String prefix, FieldSerializer.CachedField cachedField, int position)
public FieldSerializer.CachedField getField(java.lang.String fieldName)
public void removeField(java.lang.String fieldName)
public void removeField(FieldSerializer.CachedField field)
public FieldSerializer.CachedField[] getFields()
public FieldSerializer.CachedField[] getCopyFields()
public java.lang.Class getType()
public Kryo getKryo()
protected T createCopy(Kryo kryo, T original)
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)
.public T copy(Kryo kryo, T original)
Serializer
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.
copy
in class Serializer<T>
Copyright © 2019. All Rights Reserved.