How does Java’s serialization work and when it should be used instead of some other persistence technique?

I would personally try to avoid Java’s “built-in” serialization:

  • It’s not portable to other platforms
  • It’s not hugely efficient
  • It’s fragile – getting it to cope with multiple versions of a class is somewhat tricky. Even changing compilers can break serialization unless you’re careful.

For details of what the actual bytes mean, see the Java Object Serialization Specification.

There are various alternatives, such as:

  • XML and JSON, as you’ve shown (various XML flavours, of course)
  • YAML
  • Facebook’s Thrift (RPC as well as serialization)
  • Google Protocol Buffers
  • Hessian (web services as well as serialization)
  • Apache Avro
  • Your own custom format

(Disclaimer: I work for Google, and I’m doing a port of Protocol Buffers to C# as my 20% project, so clearly I think that’s a good bit of technology 🙂

Cross-platform formats are almost always more restrictive than platform-specific formats for obvious reasons – Protocol Buffers has a pretty limited set of native types, for example – but the interoperability can be incredibly useful. You also need to consider the impact of versioning, with backward and forward compatibility, etc. The text formats are generally hand-editable, but tend to be less efficient in both space and time.

Basically, you need to look at your requirements carefully.

Leave a Comment