Serializable Classes
Annotate models.
Serializable Classes is a free Kotlin Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, and your progress syncs across the web and the CoddyKit app. The Kotlin Academy course includes 4 lessons in total.
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.
Learn Kotlin with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “Serializable Classes” lesson free?
Yes — any 3 lessons of the Kotlin Academy learning path, including “Serializable Classes”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Kotlin Academy course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.
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