@SerialName und benutzerdefiniertes Field-Mapping
Kotlin-Namen an abweichende JSON-Schlüssel anpassen
@SerialName und benutzerdefiniertes Field-Mapping ist eine kostenlose Kotlin Multiplatform Academy-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Kotlin Multiplatform Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Kotlin Multiplatform Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
Names Do Not Always Match
Real APIs love keys like user_name, but Kotlin prefers userName. @SerialName lets the two disagree peacefully.
Rename a Single Field
Put @SerialName above a property to tell the serializer which JSON key it really maps to.
@SerialName("user_name") val userName: StringKotlin Stays Idiomatic
Your code keeps clean camelCase names while the wire format keeps whatever the backend chose. Both sides win.
Snake Case Is Common
Many JSON APIs use snake_case, so mapping created_at to a createdAt property is one of the most frequent uses.
@SerialName("created_at") val createdAt: StringMap Reserved Words
If JSON has a key like type that clashes with your style, @SerialName frees you to name the property whatever reads best.
@SerialName("type") val kind: StringWorks on Every Field
You can annotate as many properties as you like, mixing renamed and default-named fields inside the same data class.
Rename Enum Entries Too
@SerialName also works on enum constants, so an ACTIVE entry can serialize as the lowercase string the API expects.
@SerialName("active") ACTIVEGlobal Naming Strategy
If every key is snake_case you can set a JsonNamingStrategy once instead of annotating each field by hand.
Json { namingStrategy = JsonNamingStrategy.SnakeCase }Annotation Wins
When both a global strategy and an explicit @SerialName apply, the annotation takes priority for that one field.
Same Class, Both Ways
The custom names apply to both encoding and decoding, so one annotated model handles reading and writing JSON.
Decoupled From the Backend
Mapping keys explicitly means a backend rename only touches one annotation, not your whole codebase.
Quick Check
How do you map a JSON key to a differently named Kotlin property?
Recap
Use @SerialName to map mismatched JSON keys to idiomatic Kotlin names on fields and enums, or set a global naming strategy for whole models.
Häufig gestellte Fragen
Ist die Lektion „@SerialName und benutzerdefiniertes Field-Mapping“ kostenlos?
Ja — der vollständige Text von „@SerialName und benutzerdefiniertes Field-Mapping“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Kotlin Multiplatform Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Kotlin Multiplatform Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „@SerialName und benutzerdefiniertes Field-Mapping“?
Kotlin-Namen an abweichende JSON-Schlüssel anpassen Du übst Kotlin Multiplatform Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Kotlin Multiplatform Academy zu starten?
Keine Vorkenntnisse erforderlich. Kotlin Multiplatform Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.
Wie lange dauert die Lektion „@SerialName und benutzerdefiniertes Field-Mapping“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Kotlin Multiplatform Academy-Lektion Code schreiben und ausführen?
Ja. Jede Kotlin Multiplatform Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- @Serializable-Data-Models
- @SerialName und benutzerdefiniertes Field-Mapping
- Nullable-, Default- und optionale Felder
- Serialization in Ktor integrieren