Clone this wiki locally
Since one of Tahrir's goals is to use as little bandwidth as possible, it is important that it have an efficient data representation. Google Gson is a wonderfully easy-to-use serialization tool that converts ordinary Java objects to JSON, and back again. Unfortunately for us, JSON is not especially space efficient, even when compressed.
So, we decided to implement something similar that can take Java objects and serialize them in an extremely efficient way to raw bytes. Here are a few examples of how it compared to the stock serialization (both without any additional compression), taken from the unit tests:
Primitive types serialized to 63 bytes, compared to 142 bytes for stock serialization. Collections types serialized to 53 bytes, compared to 379 bytes for stock serialization. Object types serialized to 14 bytes, compared to 280 bytes for stock serialization.
This is a very rudimentary benchmark, but as you can see, Tahrir serialization achieves anything from a 2X to a 20X improvement over stock Java serialization, and its just as easy to use, perhaps even easier!
All code within the tahrir.io.serialization package is released under the Lesser Gnu Public License version 3.0, see here. You'll find that it is very self-contained, so if someone wants to add the serialization code to a public Maven repository, please let us know and we'll advertise it here. Of course, as with all Tahrir source code, patches are always welcome.
This serialization framework is not intended to serialize arbitrary Java objects, if that is what you need you should look at Kyro. As with Google Gson, some objects can only be serialized if you explicitly tell Tahrir how to do it. In particular, Tahrir cannot automatically serialize objects that contain circular references. That being said, dedicated serializers are provided for common collections types, including all subclasses of Collection and Map, and it is easy to add more. In practice, Tahrir can serialize most things you might want to serialize out of the box.
Also, Tahrir cannot automatically serialize classes with generic types, although common Java interfaces like Map and Connection already have built-in support.
If you want to serialize an unsupported type of object, you just need to create and register a TahrirSerializer for it, its very easy.
Simple usage example
See the Serialization unit tests for some examples of how to use Tahrir serialization.
The source code for Tahrir's serialization package can be found here.