Serializable Classes
Annotate models.
Serializable Classes is a free Kotlin Academy lesson on CoddyKit — lesson 2 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 Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The @Serializable Annotation
To make a class serializable, annotate it with @Serializable. The compiler plugin then generates a serializer for it.
import kotlinx.serialization.Serializable
@Serializable
data class User(val id: Int, val name: String)Works with Data Classes
You can annotate any class, but data classes are the most common choice for models because they auto-generate equals, hashCode, and toString.
All constructor properties become serialized fields.
Default Values
Properties with default values become optional during decoding. If the JSON omits them, the default is used.
@Serializable
data class Config(
val host: String,
val port: Int = 8080,
val secure: Boolean = false
)Nullable Properties
Nullable types map to JSON null. A nullable property without a default is still required in JSON (but its value may be null).
@Serializable
data class Profile(
val name: String,
val bio: String? = null
)Renaming Fields with @SerialName
JSON field names often differ from Kotlin naming conventions. Use @SerialName to map them.
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
data class Account(
@SerialName("user_id") val userId: Int,
@SerialName("full_name") val fullName: String
)Ignoring Properties with @Transient
Mark a property @Transient to exclude it from serialization. Transient properties must have a default value.
import kotlinx.serialization.Transient
import kotlinx.serialization.Serializable
@Serializable
data class Session(
val token: String,
@Transient val cachedAt: Long = 0L
)Nested Serializable Classes
Serializable classes can contain other serializable classes. The plugin handles the whole graph automatically.
@Serializable
data class Address(val city: String, val zip: String)
@Serializable
data class Company(val name: String, val address: Address)Collections in Models
Standard collections like List, Set, and Map are serializable out of the box when their elements are serializable.
@Serializable
data class Team(
val name: String,
val members: List<String>,
val scores: Map<String, Int>
)Enums Are Supported
Enum classes can be annotated with @Serializable and serialize to their name by default. Use @SerialName on entries to customize.
@Serializable
enum class Role {
@SerialName("admin") ADMIN,
@SerialName("user") USER
}Required vs Optional Summary
A property is required in JSON unless it has a default value. Key rules:
- No default → required
- Has default → optional
@Transient→ ignored, must have default- Nullable without default → required but may be null
What Cannot Be Serialized Directly
Types without a built-in serializer (e.g. java.util.Date, java.time.Instant, UUID) need a custom serializer, which you'll learn later.
Trying to serialize them without one causes a compile error.
Quick Check
Which statement about @Transient is correct?
Recap
You learned to annotate models:
@Serializableon the class- Default values make fields optional
@SerialNamerenames JSON keys@Transientexcludes a field (needs a default)
Next: actually encoding and decoding JSON.
Frequently asked questions
Is the “Serializable Classes” lesson free?
Yes — the full text of “Serializable Classes” is free to read here on the web, and the Kotlin 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 Academy course, upgrade to CoddyKit PRO.
What will I learn in “Serializable Classes”?
Annotate models. You practise Kotlin 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 Academy?
No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Serializable Classes” 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 Academy lesson?
Yes. Every Kotlin 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
- kotlinx.serialization Setup
- Serializable Classes
- JSON Encoding and Decoding
- Custom Serializers