Class MapSerializer<T extends Map>
- java.lang.Object
-
- com.esotericsoftware.kryo.Serializer<T>
-
- com.esotericsoftware.kryo.serializers.MapSerializer<T>
-
- Direct Known Subclasses:
DefaultSerializers.ConcurrentSkipListMapSerializer,DefaultSerializers.TreeMapSerializer,EnumMapSerializer,ImmutableCollectionsSerializers.JdkImmutableMapSerializer
public class MapSerializer<T extends Map> extends Serializer<T>
Serializes objects that implement theMapinterface.With the default constructor, a map requires a 1-3 byte header and an extra 4 bytes is written for each key/value pair.
- Author:
- Nathan Sweet
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceMapSerializer.BindMap
-
Constructor Summary
Constructors Constructor Description MapSerializer()
-
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, int size)Used byread(Kryo, Input, Class)to create the new object.protected TcreateCopy(Kryo kryo, T original)ClassgetKeyClass()SerializergetKeySerializer()ClassgetValueClass()SerializergetValueSerializer()Tread(Kryo kryo, Input input, Class<? extends T> type)Reads bytes and returns a new object of the specified concrete type.voidsetKeyClass(Class keyClass)The concrete class of the keys for this map, or null if it is not known.voidsetKeyClass(Class keyClass, Serializer keySerializer)Sets bothsetKeyClass(Class)andsetKeySerializer(Serializer).voidsetKeysCanBeNull(boolean keysCanBeNull)voidsetKeySerializer(Serializer keySerializer)The serializer to be used for the keys in this map, or null to use the serializer registered withKryofor the type.voidsetValueClass(Class valueClass)The concrete class of the values for this map, or null if it is not known.voidsetValueClass(Class valueClass, Serializer valueSerializer)Sets bothsetValueClass(Class)andsetValueSerializer(Serializer).voidsetValuesCanBeNull(boolean valuesCanBeNull)voidsetValueSerializer(Serializer valueSerializer)The serializer to be used for this field, or null to use the serializer registered withKryofor the type.voidwrite(Kryo kryo, Output output, T map)Writes the bytes for the object to the output.protected voidwriteHeader(Kryo kryo, Output output, T map)Can be overidden to write data needed forcreate(Kryo, Input, Class, int).-
Methods inherited from class com.esotericsoftware.kryo.Serializer
getAcceptsNull, isImmutable, setAcceptsNull, setImmutable
-
-
-
-
Method Detail
-
setKeysCanBeNull
public void setKeysCanBeNull(boolean keysCanBeNull)
- Parameters:
keysCanBeNull- False if all keys are not null. This saves 1 byte per key if keyClass is set. True if it is not known (default).
-
setKeyClass
public void setKeyClass(Class keyClass)
The concrete class of the keys for this map, or null if it is not known. This saves 1-2 bytes. Only set to a non-null value if the keys for this map are known to be of the specified type (or null).
-
getKeyClass
public Class getKeyClass()
-
setKeyClass
public void setKeyClass(Class keyClass, Serializer keySerializer)
Sets bothsetKeyClass(Class)andsetKeySerializer(Serializer).
-
setKeySerializer
public void setKeySerializer(Serializer keySerializer)
The serializer to be used for the keys in this map, or null to use the serializer registered withKryofor the type. Default is null.
-
getKeySerializer
public Serializer getKeySerializer()
-
setValueClass
public void setValueClass(Class valueClass)
The concrete class of the values for this map, or null if it is not known. This saves 1-2 bytes. Only set to a non-null value if the values for this map are known to be of the specified type (or null).
-
getValueClass
public Class getValueClass()
-
setValueClass
public void setValueClass(Class valueClass, Serializer valueSerializer)
Sets bothsetValueClass(Class)andsetValueSerializer(Serializer).
-
setValueSerializer
public void setValueSerializer(Serializer valueSerializer)
The serializer to be used for this field, or null to use the serializer registered withKryofor the type. Default is null.
-
getValueSerializer
public Serializer getValueSerializer()
-
setValuesCanBeNull
public void setValuesCanBeNull(boolean valuesCanBeNull)
- Parameters:
valuesCanBeNull- True if values are not null. This saves 1 byte per value if keyClass is set. False if it is not known (default).
-
write
public void write(Kryo kryo, Output output, T map)
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 extends Map>map- May be null ifSerializer.getAcceptsNull()is true.
-
writeHeader
protected void writeHeader(Kryo kryo, Output output, T map)
Can be overidden to write data needed forcreate(Kryo, Input, Class, int). The default implementation does nothing.
-
create
protected T create(Kryo kryo, Input input, Class<? extends T> type, int size)
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)with a special case for HashMap.
-
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 extends Map>- Returns:
- May be null if
Serializer.getAcceptsNull()is true.
-
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 extends Map>
-
-