Kotlinx Serialization

Egbai Smile Mmumene
ProAndroidDev
Published in
3 min readJan 10, 2019

--

After working on a multiplatform library which builds a .framework and .aar artifact, I came to the realisation that there are a lot of nifty things which are included for us in kotlin that most of us never really knew about.

One thing about working on a multiplatform project is you have to be careful about the libraries you use, and it is best to stick closely to what is offered by the kotlin team. So when I had a situation where I needed to serialize JSON which should supposedly be for the both platforms(android & ios) I had a problem on compiling my code for ios. That’s when I did a bit of digging around and found out about the Kotlinx Serialization library. To be frank I never knew about it, so this post is basically for people like me who didn’t know of it’s existence.

First of according to the kotlinx.serialization repo, this is a compiler plugin which produces visitor code for classes and has a runtime library which uses generated code to serialize objects without reflection.

The fact that this does not use reflection is a big plus in app performance, to learn more about reflection overhead you can checkout this excerpt.

Setting up your project to use the plugin is quiet simple, but I will include a complete setup for both android projects as well as multiplatform projects. But feel free to check out the Kotlinx.Serializer repo for instructions on this and more.

One small hiccup on the multiplatform serialisation setup is the need to add this dependency under the native dependecy section of your gradle script.

implementation org.jetbrains.kotlinx:kotlinx-serialization-runtime-native:0.9.1

SETUP GRADLE EXAMPLE

For multiplatform:

For Android:

Serialization

Serializing is done quite easily, for one you would need to annotate the intended class with the @Serializable annotation as below:

Note: This can as well work with data classes.

Now to actually use this in action let’s take an example of how to convert a JSON to object and back

The ease and simplicity which the kotlinx serializer helps us to convert objects in a clean way is absolutely amazing.

Two more annotations to note are transient and optional. Using transient will have the serializer ignore that field and using optional will allow the serializer not to break if a field is missing, but at the same time a default value will need to be provided.

Android Use Case for Retrofit

For those who would like to as well use this library in android, Retrofit 2 has an adapter for converting JSON responses to objects. The adapter can be found here. Some Sample code of adding the adapter

Assuming your class already has the required annotations, after making a request this should serialise your class into the required object.

All in all, the kotlin serializer is a wonderful addition to any project and makes serialization a painless process when you want to save objects as a string or parse them into other data structures. Here is a sample repo for the android setup with a small(non-architectural) working example:

Here is the official Serializer repo and as well the retrofit adapter

--

--