Recently, I was working on a project in which I need to work with a lot of enums. When working with enums I need to serialize and deserialize the enum values with Kotlinx Serialization library. I gathered a couple of cool ways for enum serialization and deserialization with Kotlinx Serialization library.
To find out the basics of Kotlinx Serialization check out the following article.
Let’s take look at them one by one.
Simple Enum Serialization & Deserialization With Kotlinx Serialization
Serialization and Deserialization of enum class is a bit more complicated but in this article, you’ll find a cut-paste recipe. Now let’s say I have this following CardType enum class which represents different types of cards.
You see I’m adding the @Serializable annotation to CardType enum. Well, that’s because the Kotlinx Serializer doesn’t know how to serialize the enum classes. That’s why we’re giving instructions to use CardTypeSerializer instead of default serializer.
Now we need to write our own serialization technique for CardType enum class.
Here’s the step-by-step explanation of the above code.
- Giving the instructions to serialization plugin to turn this class into serializer for the specified class. In our case, we’re specifically telling that this serializer is only for CardType enum class by adding forClass property.
- Picking up the StringDescriptor for encoding and decoding the enum class.
- Parse the encoded json into CardType enum class.
- Convert the enum into to string using the value encoder.
Now the only thing left to do is to test our enum class serialization.
Cool! that’s what we wanting to achieve enum classes serialization and deserialization right.
Custom Name For Enum Class Serialization & Deserialization with CommonEnumSerializer
It’s a quite common case when deserialization of json we want to parse some property to the enum constant. Like our custom name for enum serialization and deserialization.
Now let’s consider that we have the same CardType enum class and we want it to encode/decode with our custom name. Everything of the above example remains the same, we just need to change the implementation of CardTypeSerializer class.
The CommonEnumSerializer takes the T type class as for generic and in the constructor, it takes three-parameter. Let’s see the detail look on the constructor parameter.
- serialName: The serialName for overriding serializer name with our custom name.
- choices: As a second parameter, you need to pass an Array of enums which you want to encode and decode using kotlinx serialization. In our case, we’re passing all of the CardType enums because we want all of our enums to be serialized. If you want to see the documentation of enum.values method check out this link.
- choicesNames: As a third parameter, we are passing the custom names for our CardType enums. Now the
SILVERcard uses the name
My Silver Card, the
GOLDcard will be used
Gold Cardname and last
Platinumname for encoding/decode.
You see now it showing our own enum serialization names instead of default enum names.
The above example of custom naming convention for enum serialization and deserialization works fine but it’s hackiness makes me go “ICK!”. Currently, we’ve three enums and we’re passing names for every enum and maybe later we need to have four enums then we need to change our CardTypeSerialzer and add a new custom name for newly added enum.
To make the above example works in a conical way we need to have something like, if we add or modify the enum class then we don’t need to change our serializer.
Serialization & Deserialization of Enum class for parameterize constructor with CommonEnumSerializer
Let’s start this method of enum encoding/decoding by updating the CardType enum class.
Below is the explanation of the above code:
- The SerialEnum is an interface with only serialName abstract value in it. As we know an enum class can implement an interface so we override the serialName value as a constructor parameter.
- Passing the custom serialName for every CardType enum instance otherwise keep it as default which is null. Since every CardType has its own serialName which will be used for custom-named encoding/decoding of enum instance.
Next, here is the implementation of SerialEnum interface.
Before showing you the updated CardTypeSerializer class I want you to add the following extension function for Array class.
The above serial extension function for Array class accepts the T type class enum which only implements the SerialEnum interface. Inside the serial method, we apply map transformation to every enum and return the serialName if it is not null else we return the name of the enum.
Next, let’s see the updated CardTypeSerializer class.
Finally, here the example code of generic enum serialization and deserialization with the parameterized constructor:
As you can see in the output section that the silver card using the parameterized name for serialization and deserialization while on the other-hand platinum card just showing its own name because we didn’t pass any parameter for it in CardType enum class.
You can find the complete discussion of “SerialName for Enums” when working with Kotlinx Serialization library here on this link.
That’s it for now. This is my knowledge on how we can serialize and deserialize our enums class when working with Kotlinx Serialization library. If you’ve any suggestions to modify the above method please let me know in the comments section.
Thank you for being here and keep reading 🙂 …