What is the best approach for serializing java object graphs?
My requirements for serialization library are 1) speed of deserialization 2) size - as small as possible (smaller than in java default serialization) 3) flexibility - annotation based definitions of what has to be serialized would be nice.
the underlying file format is not important.
I looked at Protocol Buffers and XStream, but the former is not flexible enough due to the need for mapping files and the later produces big files.
Any help appreciated.
For small objects, the Java serialised form is likely to be dominated by the description of the serialised classes.
You may be able to write out serialised data for commonly used classes, and then use that as a common prefix for a series of serialised streams. Note that this is very fragile, and you'll probably want to recompute and check it for each class loader instance.
For serialization Hessian is one of the most efficient.
This is about 2-3 times smaller and faster than Java Serialization, even using Externalizable classes.
Whichever serialization you use, you can use compression fairly easily to make the data more compact.
Beyond that you can write your own serialization. I wrote a serializer which writes to/from ByteBuffer which is about twice as fast and half the size of Hessian (about 5x faster/smaller than Java Serialization) This may be too much effort for little gain if existing serializations will do what you need. However it is as customizable as you like ;)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With