Serialization in Ktor integrieren
ContentNegotiation für automatische Decodierung installieren
Serialization in Ktor integrieren ist eine kostenlose Kotlin Multiplatform Academy-Lektion auf CoddyKit. Dies ist Lektion 4 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.
Stop Parsing by Hand
You could decode every response manually, but Ktor can wire serialization in so requests and replies become typed automatically.
Meet ContentNegotiation
The ContentNegotiation plugin teaches the HttpClient how to convert request and response bodies to and from your models.
Add the Dependency
First add the Ktor content-negotiation artifact plus the JSON serializer module to commonMain.
implementation("io.ktor:ktor-client-content-negotiation:2.3.12")Add the JSON Bridge
You also include the ktor-serialization-kotlinx-json module, which connects Ktor to kotlinx.serialization specifically.
implementation("io.ktor:ktor-serialization-kotlinx-json:2.3.12")Install on the Client
Inside the HttpClient builder you install ContentNegotiation and register the json converter once.
install(ContentNegotiation) { json() }Configure the Json
The json block accepts a configured Json instance, so you can pass ignoreUnknownKeys and friends right here.
json(Json { ignoreUnknownKeys = true })Decode Automatically
Now a GET can return your model directly. Ktor reads the body and hands you a typed object with no manual decode call.
val user: User = client.get(url).body()Encode Request Bodies
The same plugin serializes outgoing data, so you can setBody with a Kotlin object and Ktor writes the JSON for you.
post(url) { setBody(user) }Set the Content Type
When sending a body, declare contentType as application/json so the server reads it correctly.
contentType(ContentType.Application.Json)One Setup, Both Apps
Because all of this lives in commonMain, Android and iOS share the identical typed networking with zero duplicate code.
Clean End to End
Ktor plus serialization gives you a tidy pipeline: call an endpoint, get a model, send a model, all type-safe across platforms.
Quick Check
Which Ktor plugin enables automatic JSON decoding of responses?
Recap
Install ContentNegotiation with json() on your HttpClient so Ktor auto-encodes request bodies and decodes responses into typed models on both apps.
Häufig gestellte Fragen
Ist die Lektion „Serialization in Ktor integrieren“ kostenlos?
Ja — der vollständige Text von „Serialization in Ktor integrieren“ 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 „Serialization in Ktor integrieren“?
ContentNegotiation für automatische Decodierung installieren 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 4 von 4.
Wie lange dauert die Lektion „Serialization in Ktor integrieren“?
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