com.esotericsoftware.kryo.serialize
Class CompatibleFieldSerializer

java.lang.Object
  extended by com.esotericsoftware.kryo.Serializer
      extended by com.esotericsoftware.kryo.serialize.CompatibleFieldSerializer

public class CompatibleFieldSerializer
extends Serializer

Serializes objects using direct field assignment, with limited support for forward and backward compatibility. Fields can be added or removed without invalidating previously serialized bytes. Note that changing the type of a field is not supported.

There is additional overhead compared to FieldSerializer. A header is output the first time an object of a given type is serialized. The header consists of an int for the number of fields, then a String for each field name. Also, to support skipping the bytes for a field that no longer exists, for each field value an int is written that is the length of the value in bytes.

Author:
Nathan Sweet

Nested Class Summary
 class CompatibleFieldSerializer.CachedField
          Controls how a field will be serialized.
 
Constructor Summary
CompatibleFieldSerializer(Kryo kryo, java.lang.Class type)
           
 
Method Summary
 CompatibleFieldSerializer.CachedField getField(java.lang.String fieldName)
          Allows specific fields to be optimized.
<T> T
readObjectData(java.nio.ByteBuffer buffer, java.lang.Class<T> type)
          Reads an object from the buffer.
protected
<T> T
readObjectData(T object, java.nio.ByteBuffer buffer, java.lang.Class<T> 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 accessed.
 void setFieldsCanBeNull(boolean fieldsCanBeNull)
          Sets the default value for CompatibleFieldSerializer.CachedField.setCanBeNull(boolean).
 void writeObjectData(java.nio.ByteBuffer buffer, java.lang.Object object)
          Writes the object to the buffer.
 
Methods inherited from class com.esotericsoftware.kryo.Serializer
isFinal, newInstance, readObject, setCanBeNull, writeObject
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CompatibleFieldSerializer

public CompatibleFieldSerializer(Kryo kryo,
                                 java.lang.Class type)
Method Detail

setFieldsCanBeNull

public void setFieldsCanBeNull(boolean fieldsCanBeNull)
Sets the default value for CompatibleFieldSerializer.CachedField.setCanBeNull(boolean).

Parameters:
fieldsCanBeNull - False if none of the fields are null. Saves 1 byte per field. True if it is not known (default).

setFieldsAsAccessible

public void setFieldsAsAccessible(boolean setFieldsAsAccessible)
Controls which fields are accessed.

Parameters:
setFieldsAsAccessible - If true, all non-transient fields (inlcuding private fields) will be serialized and set as accessible (default). If false, only fields in the public API will be serialized.

writeObjectData

public void writeObjectData(java.nio.ByteBuffer buffer,
                            java.lang.Object object)
Description copied from class: Serializer
Writes the object to the buffer.

Specified by:
writeObjectData in class Serializer
object - Cannot be null.

readObjectData

public <T> T readObjectData(java.nio.ByteBuffer buffer,
                            java.lang.Class<T> type)
Description copied from class: Serializer
Reads an object from the buffer.

Specified by:
readObjectData in class Serializer
Returns:
The deserialized object, never null.

readObjectData

protected <T> T readObjectData(T object,
                               java.nio.ByteBuffer buffer,
                               java.lang.Class<T> type)

getField

public CompatibleFieldSerializer.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.



Copyright © 2011. All Rights Reserved.