0Pricing
Kotlin Multiplatform Academy · Lektion

Nullable-, Default- und optionale Felder

Fehlendes oder unvollständiges JSON sauber verarbeiten

Nullable-, Default- und optionale Felder ist eine kostenlose Kotlin Multiplatform Academy-Lektion auf CoddyKit. Dies ist Lektion 3 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.

JSON Is Often Incomplete

Real responses skip fields or send null. Your shared models must handle missing data without crashing either app.

Make a Field Nullable

Mark a property as nullable with a trailing question mark, and the serializer happily reads a JSON null into it.

@Serializable
data class User(val bio: String?)

Null vs Missing

A key set to null and a key that is simply absent are different cases, and the library treats each one distinctly.

Give a Default Value

A property with a default can be left out of the JSON entirely, and decoding fills in your fallback.

val role: String = "member"

Optional Equals Default

In kotlinx.serialization, an optional field just means one that has a default, so absence is never an error.

Required Fields Are Strict

A non-null property with no default is required. If the JSON omits it, decoding throws a clear exception.

Nullable With a Default

You can combine both: a nullable property that defaults to null tolerates a missing key and a null value alike.

val avatar: String? = null

Skip Null on Output

Set explicitNulls to false and the encoder omits null fields instead of writing them out as null.

Json { explicitNulls = false }

Tolerate Extra Keys

APIs add fields over time. Turn on ignoreUnknownKeys so new JSON keys never break your existing models.

Json { ignoreUnknownKeys = true }

Coerce Bad Values

With coerceInputValues, a null sent for a field that has a default falls back to that default instead of failing.

Json { coerceInputValues = true }

Resilient by Design

Combining nullable types, defaults and lenient config makes your shared parsing robust against messy real-world JSON.

Quick Check

How do you let a JSON key be safely omitted from a response?

Recap

Use nullable types and defaults for missing data, and tune Json with ignoreUnknownKeys, explicitNulls and coerceInputValues for resilient parsing.

Häufig gestellte Fragen

Ist die Lektion „Nullable-, Default- und optionale Felder“ kostenlos?

Ja — der vollständige Text von „Nullable-, Default- und optionale Felder“ 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 „Nullable-, Default- und optionale Felder“?

Fehlendes oder unvollständiges JSON sauber verarbeiten 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 3 von 4.

Wie lange dauert die Lektion „Nullable-, Default- und optionale Felder“?

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

  1. @Serializable-Data-Models
  2. @SerialName und benutzerdefiniertes Field-Mapping
  3. Nullable-, Default- und optionale Felder
  4. Serialization in Ktor integrieren
← Zurück zu Kotlin Multiplatform Academy