public class Kryo
extends java.lang.Object
| Modifier and Type | Class and Description |
|---|---|
private static class |
Kryo.Closure |
static class |
Kryo.DefaultInstantiatorStrategy |
(package private) static class |
Kryo.DefaultSerializerEntry |
| Modifier and Type | Field and Description |
|---|---|
private boolean |
asmEnabled
Tells if ASM-based backend should be used by new serializer instances created using this Kryo instance.
|
private boolean |
autoReset |
private java.lang.ClassLoader |
classLoader |
private ClassResolver |
classResolver |
private ObjectMap |
context |
private int |
copyDepth |
private boolean |
copyReferences |
private boolean |
copyShallow |
private SerializerFactory |
defaultSerializer |
private java.util.ArrayList<Kryo.DefaultSerializerEntry> |
defaultSerializers |
private int |
depth |
private Generics |
genericsScope |
private ObjectMap |
graphContext |
private int |
lowPriorityDefaultSerializerCount |
private int |
maxDepth |
private java.lang.Object |
needsCopyReference |
private int |
nextRegisterID |
private static int |
NO_REF |
static byte |
NOT_NULL |
static byte |
NULL |
private IdentityMap |
originalToCopy |
private java.lang.Object |
readObject |
private IntArray |
readReferenceIds |
private static int |
REF |
private ReferenceResolver |
referenceResolver |
private boolean |
references |
private boolean |
registrationRequired |
private org.objenesis.strategy.InstantiatorStrategy |
strategy |
private StreamFactory |
streamFactory |
private java.lang.Thread |
thread |
| Constructor and Description |
|---|
Kryo()
Creates a new Kryo with a
DefaultClassResolver and a MapReferenceResolver. |
Kryo(ClassResolver classResolver,
ReferenceResolver referenceResolver) |
Kryo(ClassResolver classResolver,
ReferenceResolver referenceResolver,
StreamFactory streamFactory) |
Kryo(ReferenceResolver referenceResolver)
Creates a new Kryo with a
DefaultClassResolver. |
| Modifier and Type | Method and Description |
|---|---|
void |
addDefaultSerializer(java.lang.Class type,
java.lang.Class<? extends Serializer> serializerClass)
Instances of the specified class will use the specified serializer when
register(Class) or
register(Class, int) are called. |
void |
addDefaultSerializer(java.lang.Class type,
Serializer serializer)
Instances of the specified class will use the specified serializer when
register(Class) or
register(Class, int) are called. |
void |
addDefaultSerializer(java.lang.Class type,
SerializerFactory serializerFactory)
Instances of the specified class will use the specified factory to create a serializer when
register(Class) or
register(Class, int) are called. |
private void |
beginObject() |
<T> T |
copy(T object)
Returns a deep copy of the object.
|
<T> T |
copy(T object,
Serializer serializer)
Returns a deep copy of the object using the specified serializer.
|
<T> T |
copyShallow(T object)
Returns a shallow copy of the object.
|
<T> T |
copyShallow(T object,
Serializer serializer)
Returns a shallow copy of the object using the specified serializer.
|
boolean |
getAsmEnabled() |
java.lang.ClassLoader |
getClassLoader() |
ClassResolver |
getClassResolver() |
ObjectMap |
getContext()
Name/value pairs that are available to all serializers.
|
Serializer |
getDefaultSerializer(java.lang.Class type)
Returns the best matching serializer for a class.
|
protected Serializer |
getDefaultSerializerForAnnotatedType(java.lang.Class type) |
int |
getDepth()
Returns the number of child objects away from the object graph root.
|
Generics |
getGenericsScope() |
ObjectMap |
getGraphContext()
Name/value pairs that are available to all serializers and are cleared after each object graph is serialized or
deserialized.
|
org.objenesis.strategy.InstantiatorStrategy |
getInstantiatorStrategy() |
int |
getNextRegistrationId()
Returns the lowest, next available integer ID.
|
IdentityMap |
getOriginalToCopyMap()
Returns the internal map of original to copy objects when a copy method is used.
|
ReferenceResolver |
getReferenceResolver() |
boolean |
getReferences() |
Registration |
getRegistration(java.lang.Class type)
If the class is not registered and
setRegistrationRequired(boolean) is false, it is automatically registered
using the default serializer. |
Registration |
getRegistration(int classID) |
Serializer |
getSerializer(java.lang.Class type)
Returns the serializer for the registration for the specified class.
|
StreamFactory |
getStreamFactory() |
boolean |
isClousre(java.lang.Class type)
Returns true if the specified type is a closure.
|
boolean |
isFinal(java.lang.Class type)
Returns true if the specified type is final.
|
boolean |
isRegistrationRequired() |
protected Serializer |
newDefaultSerializer(java.lang.Class type)
Called by
getDefaultSerializer(Class) when no default serializers matched the type. |
<T> T |
newInstance(java.lang.Class<T> type)
Creates a new instance of a class using
Registration.getInstantiator(). |
protected org.objenesis.instantiator.ObjectInstantiator |
newInstantiator(java.lang.Class type)
Returns a new instantiator for creating new instances of the specified type.
|
void |
popGenericsScope() |
void |
pushGenericsScope(java.lang.Class type,
Generics generics) |
Registration |
readClass(Input input)
Reads a class and returns its registration.
|
java.lang.Object |
readClassAndObject(Input input)
Reads the class and object or null using the registered serializer.
|
<T> T |
readObject(Input input,
java.lang.Class<T> type)
Reads an object using the registered serializer.
|
<T> T |
readObject(Input input,
java.lang.Class<T> type,
Serializer serializer)
Reads an object using the specified serializer.
|
<T> T |
readObjectOrNull(Input input,
java.lang.Class<T> type)
Reads an object or null using the registered serializer.
|
<T> T |
readObjectOrNull(Input input,
java.lang.Class<T> type,
Serializer serializer)
Reads an object or null using the specified serializer.
|
(package private) int |
readReferenceOrNull(Input input,
java.lang.Class type,
boolean mayBeNull)
Returns
REF if a reference to a previously read object was read, which is stored in readObject. |
void |
reference(java.lang.Object object)
Called by
Serializer.read(Kryo, Input, Class) and Serializer.copy(Kryo, Object) before Kryo can be used to
deserialize or copy child objects. |
Registration |
register(java.lang.Class type)
Registers the class using the lowest, next available integer ID and the
default
serializer. |
Registration |
register(java.lang.Class type,
int id)
Registers the class using the specified ID and the
default serializer. |
Registration |
register(java.lang.Class type,
Serializer serializer)
Registers the class using the lowest, next available integer ID and the specified serializer.
|
Registration |
register(java.lang.Class type,
Serializer serializer,
int id)
Registers the class using the specified ID and serializer.
|
Registration |
register(Registration registration)
Stores the specified registration.
|
void |
reset()
Resets unregistered class names, references to previously serialized or deserialized objects, and the
graph context. |
void |
setAsmEnabled(boolean flag)
Tells Kryo, if ASM-based backend should be used by new serializer instances created using this Kryo instance.
|
void |
setAutoReset(boolean autoReset)
If true (the default),
reset() is called automatically after an entire object graph has been read or written. |
void |
setClassLoader(java.lang.ClassLoader classLoader)
Sets the classloader to resolve unregistered class names to classes.
|
void |
setCopyReferences(boolean copyReferences)
If true, when
copy(Object) and other copy methods encounter an object for the first time the object is copied and
on subsequent encounters the copied object is used. |
void |
setDefaultSerializer(java.lang.Class<? extends Serializer> serializer)
Sets the serializer to use when no
default serializers match an object's type. |
void |
setDefaultSerializer(SerializerFactory serializer)
Sets the serializer factory to use when no
default serializers match an object's
type. |
void |
setInstantiatorStrategy(org.objenesis.strategy.InstantiatorStrategy strategy)
Sets the strategy used by
newInstantiator(Class) for creating objects. |
void |
setMaxDepth(int maxDepth)
Sets the maxiumum depth of an object graph.
|
void |
setReferenceResolver(ReferenceResolver referenceResolver)
Sets the reference resolver and enables references.
|
boolean |
setReferences(boolean references)
If true, each appearance of an object in the graph after the first is stored as an integer ordinal.
|
void |
setRegistrationRequired(boolean registrationRequired)
If true, an exception is thrown when an unregistered class is encountered.
|
void |
setStreamFactory(StreamFactory streamFactory) |
Registration |
writeClass(Output output,
java.lang.Class type)
Writes a class and returns its registration.
|
void |
writeClassAndObject(Output output,
java.lang.Object object)
Writes the class and object or null using the registered serializer.
|
void |
writeObject(Output output,
java.lang.Object object)
Writes an object using the registered serializer.
|
void |
writeObject(Output output,
java.lang.Object object,
Serializer serializer)
Writes an object using the specified serializer.
|
void |
writeObjectOrNull(Output output,
java.lang.Object object,
java.lang.Class type)
Writes an object or null using the registered serializer for the specified type.
|
void |
writeObjectOrNull(Output output,
java.lang.Object object,
Serializer serializer)
Writes an object or null using the specified serializer.
|
(package private) boolean |
writeReferenceOrNull(Output output,
java.lang.Object object,
boolean mayBeNull) |
public static final byte NULL
public static final byte NOT_NULL
private static final int REF
private static final int NO_REF
private SerializerFactory defaultSerializer
private final java.util.ArrayList<Kryo.DefaultSerializerEntry> defaultSerializers
private final int lowPriorityDefaultSerializerCount
private final ClassResolver classResolver
private int nextRegisterID
private java.lang.ClassLoader classLoader
private org.objenesis.strategy.InstantiatorStrategy strategy
private boolean registrationRequired
private int depth
private int maxDepth
private boolean autoReset
private volatile java.lang.Thread thread
private ObjectMap context
private ObjectMap graphContext
private ReferenceResolver referenceResolver
private final IntArray readReferenceIds
private boolean references
private boolean copyReferences
private java.lang.Object readObject
private int copyDepth
private boolean copyShallow
private IdentityMap originalToCopy
private java.lang.Object needsCopyReference
private Generics genericsScope
private boolean asmEnabled
private StreamFactory streamFactory
public Kryo()
DefaultClassResolver and a MapReferenceResolver.public Kryo(ReferenceResolver referenceResolver)
DefaultClassResolver.referenceResolver - May be null to disable references.public Kryo(ClassResolver classResolver, ReferenceResolver referenceResolver)
referenceResolver - May be null to disable references.public Kryo(ClassResolver classResolver, ReferenceResolver referenceResolver, StreamFactory streamFactory)
referenceResolver - May be null to disable references.public void setDefaultSerializer(SerializerFactory serializer)
default serializers match an object's
type. Default is ReflectionSerializerFactory with FieldSerializer.newDefaultSerializer(Class)public void setDefaultSerializer(java.lang.Class<? extends Serializer> serializer)
default serializers match an object's type.
Default is FieldSerializer.newDefaultSerializer(Class)public void addDefaultSerializer(java.lang.Class type,
Serializer serializer)
register(Class) or
register(Class, int) are called.setDefaultSerializer(Class)public void addDefaultSerializer(java.lang.Class type,
SerializerFactory serializerFactory)
register(Class) or
register(Class, int) are called.setDefaultSerializer(Class)public void addDefaultSerializer(java.lang.Class type,
java.lang.Class<? extends Serializer> serializerClass)
register(Class) or
register(Class, int) are called. Serializer instances are created as needed via
ReflectionSerializerFactory.makeSerializer(Kryo, Class, Class). By default, the following classes have a default
serializer set:
| boolean | Boolean | byte | Byte | char |
| Character | short | Short | int | Integer |
| long | Long | float | Float | double |
| Double | String | byte[] | char[] | short[] |
| int[] | long[] | float[] | double[] | String[] |
| Object[] | Map | BigInteger | BigDecimal | KryoSerializable |
| Collection | Date | Collections.emptyList | Collections.singleton | Currency |
| StringBuilder | Enum | Collections.emptyMap | Collections.emptySet | Calendar |
| StringBuffer | Class | Collections.singletonList | Collections.singletonMap | TimeZone |
| TreeMap | EnumSet |
Note that the order default serializers are added is important for a class that may match multiple types. The above default serializers always have a lower priority than subsequent default serializers that are added.
public Serializer getDefaultSerializer(java.lang.Class type)
protected Serializer getDefaultSerializerForAnnotatedType(java.lang.Class type)
protected Serializer newDefaultSerializer(java.lang.Class type)
getDefaultSerializer(Class) when no default serializers matched the type. Subclasses can override this
method to customize behavior. The default implementation calls SerializerFactory.makeSerializer(Kryo, Class) using
the default serializer.public Registration register(java.lang.Class type)
default
serializer. If the class is already registered, no change will be made and the existing registration will be
returned. Registering a primitive also affects the corresponding primitive wrapper.
Because the ID assigned is affected by the IDs registered before it, the order classes are registered is important when using this method. The order must be the same at deserialization as it was for serialization.
public Registration register(java.lang.Class type, int id)
default serializer.
If the class is already registered this has no effect and the existing registration is returned. Registering a
primitive also affects the corresponding primitive wrapper.
IDs must be the same at deserialization as they were for serialization.
id - Must be >= 0. Smaller IDs are serialized more efficiently. IDs 0-8 are used by default for primitive types and
String, but these IDs can be repurposed.public Registration register(java.lang.Class type, Serializer serializer)
Because the ID assigned is affected by the IDs registered before it, the order classes are registered is important when using this method. The order must be the same at deserialization as it was for serialization.
public Registration register(java.lang.Class type, Serializer serializer, int id)
IDs must be the same at deserialization as they were for serialization.
id - Must be >= 0. Smaller IDs are serialized more efficiently. IDs 0-8 are used by default for primitive types and
String, but these IDs can be repurposed.public Registration register(Registration registration)
IDs must be the same at deserialization as they were for serialization.
Registration can be suclassed to efficiently store per type information, accessible in serializers via
getRegistration(Class).
public int getNextRegistrationId()
public Registration getRegistration(java.lang.Class type)
setRegistrationRequired(boolean) is false, it is automatically registered
using the default serializer.java.lang.IllegalArgumentException - if the class is not registered and setRegistrationRequired(boolean) is true.ClassResolver.getRegistration(Class)public Registration getRegistration(int classID)
ClassResolver.getRegistration(int)public Serializer getSerializer(java.lang.Class type)
public Registration writeClass(Output output, java.lang.Class type)
type - May be null.ClassResolver.writeClass(Output, Class)public void writeObject(Output output, java.lang.Object object)
public void writeObject(Output output, java.lang.Object object, Serializer serializer)
public void writeObjectOrNull(Output output, java.lang.Object object, java.lang.Class type)
object - May be null.public void writeObjectOrNull(Output output, java.lang.Object object, Serializer serializer)
object - May be null.public void writeClassAndObject(Output output, java.lang.Object object)
object - May be null.boolean writeReferenceOrNull(Output output, java.lang.Object object, boolean mayBeNull)
object - May be null if mayBeNull is true.public Registration readClass(Input input)
ClassResolver.readClass(Input)public <T> T readObject(Input input, java.lang.Class<T> type)
public <T> T readObject(Input input, java.lang.Class<T> type, Serializer serializer)
public <T> T readObjectOrNull(Input input, java.lang.Class<T> type)
public <T> T readObjectOrNull(Input input, java.lang.Class<T> type, Serializer serializer)
public java.lang.Object readClassAndObject(Input input)
int readReferenceOrNull(Input input, java.lang.Class type, boolean mayBeNull)
REF if a reference to a previously read object was read, which is stored in readObject. Returns a
stack size (> 0) if a reference ID has been put on the stack.public void reference(java.lang.Object object)
Serializer.read(Kryo, Input, Class) and Serializer.copy(Kryo, Object) before Kryo can be used to
deserialize or copy child objects. Calling this method is unnecessary if Kryo is not used to deserialize or copy child
objects.object - May be null, unless calling this method from Serializer.copy(Kryo, Object).public void reset()
graph context. If auto reset is true, this method is called
automatically when an object graph has been completely serialized or deserialized. If overridden, the super method must be
called.public <T> T copy(T object)
Serializer.copy(Kryo, Object).object - May be null.public <T> T copy(T object,
Serializer serializer)
Serializer.copy(Kryo, Object).object - May be null.public <T> T copyShallow(T object)
Serializer.copy(Kryo, Object).object - May be null.public <T> T copyShallow(T object,
Serializer serializer)
Serializer.copy(Kryo, Object).object - May be null.private void beginObject()
public ClassResolver getClassResolver()
public ReferenceResolver getReferenceResolver()
public void setClassLoader(java.lang.ClassLoader classLoader)
public java.lang.ClassLoader getClassLoader()
public void setRegistrationRequired(boolean registrationRequired)
If false, when an unregistered class is encountered, its fully qualified class name will be serialized and the
default serializer for the class used to serialize the object. Subsequent
appearances of the class within the same object graph are serialized as an int id.
Registered classes are serialized as an int id, avoiding the overhead of serializing the class name, but have the drawback of needing to know the classes to be serialized up front.
public boolean isRegistrationRequired()
public boolean setReferences(boolean references)
MapReferenceResolver is used. This enables references to the same object and cyclic graphs to be serialized, but
typically adds overhead of one byte per object. Default is true.public void setCopyReferences(boolean copyReferences)
copy(Object) and other copy methods encounter an object for the first time the object is copied and
on subsequent encounters the copied object is used. If false, the overhead of tracking which objects have already been
copied is avoided because each object is copied every time it is encountered, however a stack overflow will occur if an
object graph is copied that contains a circular reference. Default is true.public void setReferenceResolver(ReferenceResolver referenceResolver)
public boolean getReferences()
public void setInstantiatorStrategy(org.objenesis.strategy.InstantiatorStrategy strategy)
newInstantiator(Class) for creating objects. See StdInstantiatorStrategy to
create objects via without calling any constructor. See SerializingInstantiatorStrategy to mimic Java's built-in
serialization.strategy - May be null.public org.objenesis.strategy.InstantiatorStrategy getInstantiatorStrategy()
protected org.objenesis.instantiator.ObjectInstantiator newInstantiator(java.lang.Class type)
strategy is set, it will be used instead of throwing an exception.public <T> T newInstance(java.lang.Class<T> type)
Registration.getInstantiator(). If the registration's instantiator is null,
a new one is set using newInstantiator(Class).public ObjectMap getContext()
public ObjectMap getGraphContext()
public int getDepth()
public IdentityMap getOriginalToCopyMap()
reset() so this is only useful when
setAutoReset(boolean) is false.public void setAutoReset(boolean autoReset)
public void setMaxDepth(int maxDepth)
Integer.MAX_VALUE.public boolean isFinal(java.lang.Class type)
This can be overridden to force non-final classes to be treated as final. Eg, if an application uses ArrayList extensively but never uses an ArrayList subclass, treating ArrayList as final could allow FieldSerializer to save 1-2 bytes per ArrayList field.
public boolean isClousre(java.lang.Class type)
This can be overridden to support alternative implementations of clousres. Current version supports Oracle's Java8 only
public void pushGenericsScope(java.lang.Class type,
Generics generics)
public void popGenericsScope()
public Generics getGenericsScope()
public StreamFactory getStreamFactory()
public void setStreamFactory(StreamFactory streamFactory)
public void setAsmEnabled(boolean flag)
By default, Kryo uses ASM-based backend.
flag - if true, ASM-based backend will be used. Otherwise Unsafe-based backend could be used by some serializers, e.g.
FieldSerializerpublic boolean getAsmEnabled()