Kotlin Serialization

Sharing code across platforms is not possible. IO, transformations, threading, and serialization, to mention a few, are all handled differently on each platform you want to support. There are already several libraries that support kotlin serialization and can even produce kotlin code, with Square’s Moshi being the most well-known.

In this article, we will have a brief discussion on kotlin serialization.

What are Serialization and Deserialization in Kotlin?

Serialization is the technique of transforming an application’s data into a format that can be sent across a network or can be saved in the database or file. Deserialization, on the other hand, is the act of extracting data from a remote location and transforming it into a run-time object. They are both required components in most apps that interact with data with outside parties.

JSON and protocol buffers are two examples of data serialization formats that are widely used. They allow data to flow across systems built in any modern language because they are both language and platform agnostic.

Data serialization capabilities in Kotlin may be found in a standalone component called kotlinx. serialization. The runtime libraries and the Gradle plugin–org.jetbrains.kotlin.plugin.serialization– are the two primary components.

Kotlin Serialization Libraries

All Kotlin serialization libraries are contained in the org.jetbrains package. Kotlinx is a name for a group of people. They have suffixes that represent the serialization format and begin with kotlinx-serialization.


JSON serialization for Kotlin projects is provided by org.jetbrains.kotlinx:kotlinx-serialization-json.

CBOR serialization is provided by org.jetbrains.kotlinx: kotlinx-serialization-cbor.

You do not need to manually add platform-specific artifacts because they are handled automatically. The JVM, JS, Native, and cross-platform projects use the same dependencies.

How to Utilize Kotlin Serialization in the Code

Before you proceed with the kotlin serialization code, make sure your build script is configured to allow you to utilize Kotlin serialization tools in your project.

In your project build.gradle.kts file use the kotlin(“plugin.serialization”) in the plugin block.

Then, in your build.gradle(.kts) file, add the runtime library dependency “org.jetbrains.kotlinx: kotlinx-serialization-json: 1.0.0” to your dependents block.

To understand the Kotlin serialization, you should take a look at the following examples:

Example # 1: Program to Serialize Kotlin Instance into JSON

We have the serialization API which will help us to work on our code. The kotlinx.serialization and its feature sub packages, including kotlinx.serialization.json, include the API. There is another library “Json.encodeToString” to use, which encodes strings to JSON.

We import the libraries from the kotlinx.serialization. First, we annotate a class with @Serializable. We declare a data class here with the name “Data”. The class Data calls a constructor with two variables. The variables are represented with the keyword “val” and assign a name “name” and “age”. The variable “name” is set with the type property “String” and the variable “age” is set with “Int” type property.

After the data class declaration, we use @optIn which is a global space. Kotlin’s opt-in approach lets you identify APIs that should be utilized with cautiousness – or not at all. If you make a declaration (a class, a function, a property, or anything else) opt-in necessary, using it will result in a warning or error in the code, requiring the user to actively opt-in to use it. This will ensure that the decision to use the API on the use site was made consciously.

Here, we pass an experimental API marker to it. Then, we have the main function define in which we create a variable “myInfo”. The variable “myInfo” is called Json.encodeToString in which the object passed as a parameter is serialized and encoded to a JSON string. As a result, you’ll get a JSON-formatted string representing the object’s current state. By using the kotlin println function, we will print the values of the variables “myInfo”.

The output is in JSON format shown in the console screen below.

Example # 2: Program to Serialize and Deserialize Objects in Kotlin

Serialization is the conversion of an object to a stream of bytes or a string while deserialization is the restoration of that stream into a new object.

In the above code, we have an implementation of a Serializable interface that allows Kotlin to handle serialization automatically.

The library java.io signifies that we have imported all of the input-output package’s classes into the code. Then, we declare a class as “Players” which has a Serializable interface implemented. The class “Players” objects can now be transformed into streams. The constructor is called for the class “Players” and we create an object “players” in it which is kept private and sets a property of String type.

After that, we have a keyword “override” with the function toString(). The Override’s explicitness explains the program and avoids errors. A function toString() represents the elements of an object in a readable format is important for identifying and correcting programming problems. The override toString() will return the string of the object “players”. We have the init block in which we initialize an object “players” with the elements of string using the hashSetOf function. Then, we have another serialized class “Tennis” in which we set its objects by creating its constructor.

We create an object “player” in the class “Tennis” and call the Players class in it. In the override toString() block, we set the fields of an object player. After this, we have a main function in which we declare a variable “tennis” and set its fields. Now using the writeObject() function from the ObjectOutputStream class, convert the object to a serialized form, then use the readObject() function to generate a brand new object from the bytes.

On the terminal screen, the output is displaye.


The objective of this article guide is to show the concept of Kotlin serialization in the Kotlin language. We discussed the topic of serialization and deserialization and how to utilize the Kotlin serialization libraries. We also covered the examples with the running code for you.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.