Object Serialization in Kotlin: Library Overview and Practical Guide
Serialization is the process of converting complex objects (classes, data structures) into a format suitable for storage or network transmission (JSON, XML, Protobuf). The reverse operation — deserialization — restores an object from such a format. In Kotlin, thanks to its conciseness and type safety, serialization is implemented particularly elegantly. In this article, we will look at three main approaches: the standard library kotlinx.serialization, as well as the popular Java solutions Gson and Jackson, adapted for Kotlin.
1. kotlinx.serialization — Official Solution from JetBrains
The kotlinx.serialization library is part of the Kotlin ecosystem and is supported by the language developers. It offers a native approach without reflection, ensuring high performance and type safety at compile time.
Setup (Gradle)
plugins { kotlin("jvm") version "1.9.0" kotlin("plugin.serialization") version "1.9.0"}
dependencies { implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.0")}Example: Basic Class and Serialization
import kotlinx.serialization.Serializableimport kotlinx.serialization.json.Json
@Serializabledata class User( val name: String, val age: Int, val email: String? = null)
fun main() { val user = User("Alexey", 30, "alex@example.com") val jsonString = Json.encodeToString(user) println(jsonString) // {"name":"Alexey","age":30,"email":"alex@example.com"}
val decoded = Json.decodeFromString<User>(jsonString) println(decoded) // User(name=Alexey, age=30, email=alex@example.com)}Advantages
- Type safety: serialization errors are detected at compile time.
- Multiplatform support: works on JVM, JS, Native (KMP).
- Extensibility: custom serializers for complex types.
- Built-in support for null, enum, sealed class, collections.
Advanced Example: Nested Objects and Lists
@Serializabledata class Address(val city: String, val street: String)
@Serializabledata class Person(val name: String, val addresses: List<Address>)
fun main() { val person = Person("Maria", listOf( Address("Moscow", "Tverskaya"), Address("Saint Petersburg", "Nevsky") )) val json = Json { prettyPrint = true } println(json.encodeToString(person)) // { // "name": "Maria", // "addresses": [ // { "city": "Moscow", "street": "Tverskaya" }, // { "city": "Saint Petersburg", "street": "Nevsky" } // ] // }}2. Gson — Lightweight and Simple
The Gson library from Google is widely known in the Java world. For Kotlin, it is used with additional configuration to correctly handle data class and nullable fields.
Setup
implementation("com.google.code.gson:gson:2.10.1")Example with Gson
import com.google.gson.Gsonimport com.google.gson.reflect.TypeToken
data class Product(val id: Int, val title: String, val price: Double?)
fun main() { val gson = Gson() val product = Product(1, "Laptop", 75000.0) val json = gson.toJson(product) println(json) // {"id":1,"title":"Laptop","price":75000.0}
val back = gson.fromJson(json, Product::class.java) println(back) // Product(id=1, title=Laptop, price=75000.0)}Features
- Does not require annotations — works with any classes.
- Requires explicit type specification for deserialization (can be inconvenient with generics).
- Does not handle