@Serializable Data Models
Annotate models for compile-time-safe parsing.
@Serializable Data Models is a free Kotlin Multiplatform Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Multiplatform Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
JSON Needs a Map
APIs speak JSON, but your shared code wants Kotlin objects. kotlinx.serialization bridges that gap on every KMP target.
Compile-Time Safety
Unlike reflection-based parsers, this library generates code at compile time, so it works fast and predictably on Android and iOS.
Mark a Class @Serializable
You tag a data class with @Serializable and the compiler writes a serializer for it automatically.
@Serializable
data class User(val id: Int, val name: String)Add the Plugin
The annotation only works once you apply the kotlin-serialization Gradle plugin to your shared module.
plugins { kotlin("plugin.serialization") }Add the Runtime
You also need the JSON runtime dependency in commonMain so the generated code has something to call.
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.3")Encode to a String
Turn a model into JSON text with Json.encodeToString, ready to send over the network.
val text = Json.encodeToString(user)Decode From a String
Going the other way, decodeToString's partner reads JSON text back into a typed Kotlin object.
val user = Json.decodeFromString<User>(text)Properties Become Keys
By default each property name becomes the matching JSON key, so id and name map straight across with no extra config.
Nested Models Just Work
If a serializable class holds another serializable class, the whole nested tree serializes together with no manual wiring.
@Serializable
data class Order(val buyer: User)Supported Types
Primitives, strings, lists, maps and other serializable classes are all supported, which covers most real API payloads you will meet.
Truly Multiplatform
Because the serializer is generated Kotlin, the exact same parsing logic runs on every target with no platform-specific code.
Quick Check
What does the @Serializable annotation actually trigger?
Recap
Tag models with @Serializable, add the plugin and runtime, then use Json.encodeToString and decodeFromString to move between objects and JSON.
Frequently asked questions
Is the “@Serializable Data Models” lesson free?
Yes — the full text of “@Serializable Data Models” is free to read here on the web, and the Kotlin Multiplatform Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Multiplatform Academy course, upgrade to CoddyKit PRO.
What will I learn in “@Serializable Data Models”?
Annotate models for compile-time-safe parsing. You practise Kotlin Multiplatform Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Kotlin Multiplatform Academy?
No prior experience is required. Kotlin Multiplatform Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “@Serializable Data Models” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Kotlin Multiplatform Academy lesson?
Yes. Every Kotlin Multiplatform Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- @Serializable Data Models
- @SerialName & Custom Field Mapping
- Nullable, Default & Optional Fields
- Plug Serialization into Ktor