Object Serialization in Kotlin: A Complete Guide to Libraries and Tools
Serialization is the process of converting an object into a format that can be saved (e.g., to a file) or transmitted over a network (via REST API, Kafka messages, etc.). The reverse process — deserialization — restores the object from that format. In the Kotlin ecosystem, there are several powerful serialization libraries, each with its own features. In this article, we will cover four main approaches: kotlinx.serialization, Gson, Moshi, and Jackson.
1. kotlinx.serialization — The Native Choice for Kotlin
Developed by JetBrains, the kotlinx.serialization library is the "gold standard" for Kotlin projects. It supports multiplatform (JVM, JS, Native) and uses annotations for compile-time code generation.
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")}Usage Example
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("Ivan", 30, "ivan@example.com")
// Serialization to JSON val jsonString = Json.encodeToString(user) println(jsonString) // {"name":"Ivan","age":30,"email":"ivan@example.com"}
// Deserialization from JSON val decoded = Json.decodeFromString<User>(jsonString) println(decoded) // User(name=Ivan, age=30, email=ivan@example.com)}Features
- Null safety: support for nullable fields via
? - Polymorphism: serialization of sealed classes and hierarchies
- Custom formats: JSON, CBOR, Protobuf, Properties
- Performance: code generation without reflection
2. Gson — The Classic from Google
Gson is one of the oldest libraries for Java/Kotlin. It is simple to use but relies on reflection, which can reduce performance on large data volumes.
Setup
implementation("com.google.code.gson:gson:2.10.1")Example
import com.google.gson.Gson
data class Product(val id: Int, val title: String, val price: Double)
fun main() { val gson = Gson() val product = Product(1, "Laptop", 79999.99)
// Serialization val json = gson.toJson(product) println(json) // {"id":1,"title":"Laptop","price":79999.99}
// Deserialization val restored = gson.fromJson(json, Product::class.java) println(restored) // Product(id=1, title=Laptop, price=79999.99)}Features
- Simplicity: minimal configuration
- Flexibility: custom
TypeAdapterfor complex types - Drawbacks: reflection, no native support for Kotlin data classes (requires a plugin or
@JvmField)
3. Moshi — A Modern Alternative to Gson
Moshi from Square is a lightweight library with built-in Kotlin support. It uses code generation via annotations, making it faster than Gson.
Setup
implementation("com.squareup.moshi:moshi-kotlin:1.15.0")kapt("com.squareup.moshi:moshi-kotlin-codegen:1.15.0") // for code generationExample
import com.squareup.moshi.Moshiimport com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
@JsonClass(generateAdapter = true)data class Book(val isbn: String, val title: String, val authors: