KO

Kotlinx Serialization

Kotlin multiplatform serialization

Details

Language / Topic
kotlinKotlin
Category
Libraries
Source
Kotlin

Rules

balanced
- Annotate data classes with `@Serializable` — the compiler plugin generates serializers at compile time, no reflection needed.
- Use `Json { ignoreUnknownKeys = true }` for lenient parsing of external APIs — strict mode (default) throws on unexpected fields.
- Use `@SerialName("json_field")` for JSON field name mapping — Kotlin property names stay idiomatic (`camelCase`) while JSON uses `snake_case`.
- Configure `Json { ignoreUnknownKeys = true; encodeDefaults = false }` for flexible parsing.
- Use `@Contextual` for custom serializers on types you don't own (UUID, LocalDate).
- Use sealed classes with `@Serializable` for polymorphic JSON with discriminator field.
- Prefer kotlinx.serialization over Gson/Moshi — it's multiplatform and compile-time safe.