Reputation: 3
I'm using Kryo for creating game-saves in code I'm working on.
Using the default serializer, FieldSerializer, I serialize one massive POJO. It's a class that contains other classes I've created and primitive types.
The entire state of my game world is saved and loaded completely correct without creating any custom serializers.
The only thing I can think of is improving disk space or speed of serialization/deserialization.
Upvotes: 0
Views: 531
Reputation: 11032
Yes, in a perfect world, you should only serialize pojos, and pojos should be a 'tree of primitives data types'.. But it's not so easy.
In order to be fast, Kryo doesn't use java serialization by default. It have his own serializer, FieldSerializer
, which introspect instances and serialize it fields by fields. This serializer doesn't use default method like readResolve
writeReplace
, and fail to restore some objects behaviors.
For example, imagine a simple POJO, which own a Date
property. This "primitive datatypes" doesn't have any field serializable (there are all marked as transient) : his serialization process is done through readObject
or writeObject
: The default FieldSerializer will not work.
If your POJO has a collection property, and you create an instance with Collections.emptyList()
, then this can work.. or not : The "emptyList" collection should be a singleton, and this behavior is done through a readResolve
method. The FieldSerializer doesn't know about it.
Kryo come with a set of defaults serializers which can handle this kind of types, but sometimes, you have to create your own to handle a class with a particular behavior. But usually, you create a serializer in order to improve the speed of classes which you serialize often, not because Kryo is not able to serialize them.
Kryo come with a special serializer, JavaSerializer
or ExternalizableSerializer
which use the default java serialization but they prevent Kryo to "see" inner fields and you loose all the benefits of using Kryo.
Upvotes: 1