Introducing @AutoSerialize
In this post I’ll present a new framework for serialization in Java that I’ve been working on over the last year.
The initial goal was to implement a paradigm which I felt I’ve been duplicating many times; efficient, portable, and hassle-free serialization of objects.
I’m also strongly biased towards functional programming, so immutability plays a big role in my projects.
TinySerializer
Enter TinySerializer.
It’s a project that started out as two interfaces (Serializer
and
SerializerFramework
), but recently grew into it’s shoes through the
inspiration of projects like
AutoValue, and
AutoMatter.
I realized that annotation processors have access to a tremendous amount of information about the types and structure of your classes, and that this information could be used to generate very efficient and readable serializers.
In the following example, I’ll be using Lombok’s @Data annotation.
So, given a Person
like the following:
And a Job
:
The @AutoSerialize
processor would then generate the following serializer
for Person
:
The use of SerializerFramework
is an important detail, this gives us a lot
of freedom in deciding the exact details of how this serialization is to
operate.
In the serializer, there are no concrete implementations or static initialization. Everything is Plain Old Boring Java.
You activate the serializer would be to add the following snippet to your maven dependencies:
Implementation Details
So the above provides you with essentially free object graph serialization for your objects, but what about primitive types and containers?
To this end, I’ve built a small companion implementation in
tiny-serializer-core
which provides you with a relatively sane
SerializerFramework
implementation.
With it, you can build a new SerializerFramework
and use it with your class
like this:
You can see this a larger example in action at the SerializeToFile example.
There are also plenty of configuration options available in TinySerialize
that you can play around with.
Final Words
I hope you find it useful!
If you want to know more, go to udoprog/tiny-serializer-java. There is plenty more to read and play with there.
I also intend to eventually implement field-value based serialization that allows for out-of-order fields. This is the basis for semantically versioned objects with forward compatibility, and I consider it an important omission.