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
- kotlinx.serialization Setup
- Serializable Classes
- JSON Encoding and Decoding
- Custom Serializers