codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🎯

Kotlin

40 / 68 topics
36Kotlin Standard Library37Collections Functions38Sequences in Kotlin39Date and Time API40Kotlin Serialization41Kotlin Coroutines Library42Kotlin HTML DSL43Kotlin CLI
Tutorials/Kotlin/Kotlin Serialization
🎯Kotlin

Kotlin Serialization

Updated 2026-05-15
10 min read

Kotlin Serialization

Introduction

Serialization is the process of converting an object into a format that can be easily stored or transmitted, and deserialization is the reverse process. In the Kotlin programming language, serialization can be efficiently handled using the kotlinx.serialization library, which provides a flexible and powerful way to serialize and deserialize data.

Kotlin's kotlinx.serialization library supports various formats such as JSON, XML, and Protocol Buffers, making it versatile for different use cases. This tutorial will guide you through the basics of using kotlinx.serialization to serialize and deserialize data in Kotlin.

Concept

The kotlinx.serialization library uses annotations to mark classes that need to be serialized or deserialized. The main annotation used is @Serializable, which tells the compiler that a class can be serialized. Additionally, you can use other annotations like @SerialName to customize the serialization behavior.

To use kotlinx.serialization, you first need to add the necessary dependencies to your project. If you're using Gradle, you can add the following to your build.gradle.kts file:

Kotlin
1dependencies {
2 implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.0")
3}

Once the dependencies are added, you can start using the library in your Kotlin code.

Examples

Basic Serialization and Deserialization

Let's start with a simple example where we serialize and deserialize a Person class.

First, define the Person class with the @Serializable annotation:

Kotlin
1import kotlinx.serialization.Serializable
2
3@Serializable
4data class Person(val name: String, val age: Int)

Next, create an instance of the Person class and serialize it to JSON using the Json.encodeToString function:

Kotlin
1import kotlinx.serialization.json.Json
2
3fun main() {
4 val person = Person("John Doe", 30)
5 val json = Json.encodeToString(person)
6 println(json) // Output: {"name":"John Doe","age":30}
7}

To deserialize the JSON back into a Person object, use the Json.decodeFromString function:

Kotlin
1fun main() {
2 val jsonString = "{"name":"Jane Doe","age":25}"
3 val person: Person = Json.decodeFromString(jsonString)
4 println(person) // Output: Person(name=Jane Doe, age=25)
5}

Customizing Serialization

You can customize the serialization process using annotations like @SerialName to change the name of a property in the serialized output.

For example, let's rename the name property to fullName in the JSON output:

Kotlin
1import kotlinx.serialization.Serializable
2import kotlinx.serialization.SerialName
3
4@Serializable
5data class Person(@SerialName("fullName") val name: String, val age: Int)

Now, when you serialize a Person object, the name property will be serialized as fullName:

Kotlin
1fun main() {
2 val person = Person("John Doe", 30)
3 val json = Json.encodeToString(person)
4 println(json) // Output: {"fullName":"John Doe","age":30}
5}

Handling Complex Data Structures

kotlinx.serialization also supports complex data structures, such as lists and nested objects. Here's an example of serializing a list of Person objects:

Kotlin
1import kotlinx.serialization.Serializable
2import kotlinx.serialization.encodeToString
3import kotlinx.serialization.json.Json
4
5@Serializable
6data class Person(val name: String, val age: Int)
7
8fun main() {
9 val people = listOf(Person("Alice", 28), Person("Bob", 35))
10 val json = Json.encodeToString(people)
11 println(json) // Output: [{"name":"Alice","age":28},{"name":"Bob","age":35}]
12}

To deserialize the JSON back into a list of Person objects, use the Json.decodeFromString function:

Kotlin
1fun main() {
2 val jsonString = "[{"name":"Alice","age":28},{"name":"Bob","age":35}]"
3 val people: List<Person> = Json.decodeFromString(jsonString)
4 println(people) // Output: [Person(name=Alice, age=28), Person(name=Bob, age=35)]
5}

What's Next?

In the next section, we will explore Kotlin Coroutines Library, which provides a powerful way to handle asynchronous programming in Kotlin. This library is essential for building responsive and efficient applications.

Stay tuned for more tutorials on Kotlin and other exciting topics!


PreviousDate and Time APINext Kotlin Coroutines Library

Recommended Gear

Date and Time APIKotlin Coroutines Library