JSON Serialization
DATA › Networking
Mapping JSON to Kotlin types with kotlinx.serialization, Moshi, and Gson.
JSON serialization is a near-universal Android interview topic because every networked app parses API payloads, and the library you pick affects build speed, app size, and crash safety. Interviewers test whether you understand compile-time codegen versus runtime reflection, how each library handles missing/unknown/optional fields and defaults, and why kotlinx.serialization is now the default for new Kotlin and KMP code. Expect to compare kotlinx.serialization, Moshi, and Gson and to reason about polymorphism and custom serializers.
What this covers
- kotlinx.serialization's @Serializable, the Gradle compiler plugin, and compile-time generated serializers
- Moshi codegen (@JsonClass(generateAdapter = true) via KSP) versus its reflective KotlinJsonAdapterFactory
- Why Gson is legacy: reflection-based, ignores Kotlin nullability and default values, unmaintained
- Handling unknown keys (ignoreUnknownKeys), optional/absent fields, and default values across libraries
- Polymorphic serialization of sealed classes and writing custom KSerializer/JsonAdapter implementations
- Field renaming and exclusion: @SerialName/@Transient versus @Json(name=)/@Json(ignore=true)
Study this topic
- JSON Serialization explained: the guided lesson
- 14 practice quiz questions
- 9 revision flashcards
- JSON Serialization interview questions and answers