Parse polymorphic media JSON with Kotlin sealed classes and custom serializers
Polymorphic deserialization with Kotlin sealed classes.
Sealed Media with Image and Video; use Moshi Factory or kotlinx.serialization keyed on the type field.
Manual JSONObject branching or reflection-based parsing.
WHAT THIS TESTS: This question evaluates whether you can bridge runtime JSON shape variation to compile-time type safety. Interviewers want to see that you understand discriminated unions, sealed class hierarchies, and how to configure a serialization library to resolve concrete types from a discriminator field rather than falling back to reflection or imperative parsing.
A GOOD ANSWER COVERS: First, define a sealed class Media with data class Image(val url: String) and data class Video(val source: String) as subclasses. Second, explain that kotlinx.serialization supports closed polymorphism out of the box for sealed classes by default using a type discriminator, but the JSON key must match or be customized with JsonClassDiscriminator or a custom serializer module. Third, for Moshi, describe building a JsonAdapter.Factory that inspects the type string in the JsonReader, peeks the value, then delegates to the correct adapter for Image or Video. Fourth, mention error handling for unknown types, such as returning a default subtype, skipping, or throwing a specific ParseException rather than a generic crash. Fifth, note that the adapter must be registered on the Moshi.Builder or the SerializersModule must be passed to the Json instance before parsing the list.
COMMON WRONG ANSWERS: A major red flag is suggesting manual JSONObject traversal with getString("type") and nested when blocks outside of the serialization layer. Another is using reflection to instantiate classes from the type string, which breaks obfuscation and compile-time safety. Candidates also stumble by claiming Moshi cannot do polymorphism natively; while it lacks built-in sealed support, a Factory is the idiomatic solution. Similarly, saying kotlinx.serialization requires no setup for sealed classes is partially true for basic cases, but custom discriminator keys or unknown type handling still require explicit configuration.
LIKELY FOLLOW-UPS: The interviewer may ask how you would handle an unknown type field without crashing the entire list, which points to a default polymorphic type handler or a fallback UnknownMedia object. They might also ask how to serialize back to JSON with the same discriminator, or how this approach scales to twenty subtypes, which is where a map-based Factory or generated serializers shine. Performance questions sometimes appear, such as the cost of peeking in Moshi or the binary size impact of kotlinx.serialization polymorphic descriptors.
ONE CONCRETE EXAMPLE: With kotlinx.serialization, you annotate Media with the Serializable annotation and configure a Json block with serializersModule set to a SerializersModule that registers polymorphic Media with subclass Image and subclass Video. The JSON output includes a type field equal to Image by default. With Moshi, you write a MediaAdapterFactory that reads the type field, then returns an imageAdapter or videoAdapter accordingly, and you install it by adding the factory to Moshi.Builder and calling build. Both approaches let you parse a list of Media in a single call while preserving exhaustiveness checks in a when expression.
Read the original → github.com
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.