0Pricing
Kotlin Academy · Lesson

Serializable Classes

Annotate models.

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.

All lessons in this course

  1. kotlinx.serialization Setup
  2. Serializable Classes
  3. JSON Encoding and Decoding
  4. Custom Serializers
← Back to Kotlin Academy