Which is the best alternative for Java Serialization?
It's 2011, and in a commercial grade REST web services project we use the following serializers to offer clients a variety of media types:
- XStream (for XML but not for JSON)
- Jackson (for JSON)
- Kryo (a fast, compact binary serialization format)
- Smile (a binary format that comes with Jackson 1.6 and later).
- Java Object Serialization.
We experimented with other serializers recently:
- SimpleXML seems solid, runs at 2x the speed of XStream, but requires a bit too much configuration for our situation.
- YamlBeans had a couple of bugs.
- SnakeYAML had a minor bug relating to dates.
Jackson JSON, Kryo, and Jackson Smile were all significantly faster than good old Java Object Serialization, by about 3x to 4.5x. XStream is on the slow side. But these are all solid choices at this point. We'll keep monitoring the other three.
of which implementation we don't have any control
The solution is don't do this. If you don't have control of a type's implementation you shouldn't be serialising it. End of story. Java serialisation provides serialVersionUID specifically for managing serialisation incompatibilities between different versions of a type. If you don't control the implementation you cannot be sure that IDs are being changed correctly when a developer changes a class.
Take a simple example of a 'Point'. It can be represented by either a cartesian or a polar coordinate system. It would be cost prohibitive for you to build a system that could cope dynamically with these sorts of corrections - it really has to be the developer of the class who designs the serialisation.
In short it's your design that's wrong - not the technology.
http://x-stream.github.io/ is nice, please take a look at it! Very convenient