Kotlinx.serialization: Kotlin's Native Data Converter

Kotlinx.serialization is Kotlin's native way to convert data classes into formats like JSON. It uses a compiler plugin to automate the process, making it ideal for API calls or saving data.
Why it exists
Applications constantly need to send data over a network or store it in a file. This requires converting in-memory objects, like a Kotlin data class, into a standard format like JSON and then back again. Kotlinx.serialization provides a first-party, type-safe, and performant way to handle this directly in Kotlin without relying on third-party libraries that use reflection.
The mental model
Think of kotlinx.serialization as a built-in translator for your Kotlin data classes. You annotate a class with @Serializable, and a compiler plugin automatically writes the conversion logic for you. You give the library an object, and it gives you back a JSON string. You give it a JSON string and a type, and it gives you back a matching object.
How it works
The process has three parts. First, apply the kotlin("plugin.serialization") plugin in your Gradle build script. Second, add a dependency for the specific format you need, like kotlinx-serialization-json. Third, annotate your data class with @Serializable. The compiler plugin then generates the necessary code behind the scenes. To use it, you call functions like Json.encodeToString(myObject) to serialize and Json.decodeFromString<MyClass>(jsonString) to deserialize.
When to use it
Use kotlinx.serialization for most standard serialization tasks in a modern Kotlin project. It is the idiomatic choice for consuming JSON APIs in Android apps, building Ktor backends, or writing multiplatform code that needs to share data models and serialization logic across JVM, JavaScript, and Native targets.
When not to use it
If you need to work with a format not officially supported (like YAML or Avro), you'll need a community-maintained or third-party library. Be cautious using the non-JSON formats like Protobuf or CBOR in production, as their APIs are marked Experimental and can change. For legacy projects with heavy investment in other libraries like Gson or Moshi, a full migration might not be practical.
One canonical example
To serialize a project object, first define the class: @Serializable data class Project(val name: String, val active: Boolean). Then, create an instance and encode it: val project = Project(name = "KMP App", active = true). The call Json.encodeToString(project) produces the JSON string {"name":"KMP App","active":true}. To reverse the process, Json.decodeFromString<Project>("{\"name\":\"KMP App\",\"active\":true}") recreates the original Project object.
Interview question
What is a key characteristic that distinguishes Kotlinx.serialization from reflection-based serialization libraries?
- a.It requires manual implementation of serialization interfaces for each data class.
- b.It generates type-safe serialization code during compilation, avoiding runtime reflection.Correct
- c.It performs data conversion dynamically at runtime using reflection.
- d.It is exclusively designed for converting data to and from XML format.
Why? this is the answer
Kotlinx.serialization uses a compiler plugin to generate conversion logic at compile time, making it type-safe and performant without relying on runtime reflection. The most tempting distractor suggests it uses reflection, which is precisely what it aims to avoid.
Just read this? Test yourself on what you have been reading.
Read the original → kotlinlang.org
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on kotlin — each one lists the topics its interview covers.
See open roles