skip to content

Serialization Formats

The concrete formats — JSON, binary encodings — plus custom serializers, polymorphic encoding, and evolving a schema without breaking old clients. Compatibility is the part interviewers actually care about.

part ofKotlinoverview, primer and where to startread it →
on this pageshow

explore

questions

page 2 of 2

kotlinx.serialization supports more than one way of laying out polymorphic data on the wire. Explain ARRAY_WRAPPED vs the discriminator object mode, and when you must use which.

level: seniorimportance: should knowfreq 35%

basics

~20 s

Normally the type name is a field inside the object (discriminator mode). But that only works when the value is a JSON object. For non-object values (like a primitive), the library wraps it as a two-element array [typeName, value]. You can also force array mode.

open as a page

When hand-writing a serializer, how do you correctly handle nested serializable values and nullable fields? Contrast encodeSerializableValue with encodeNullableSerializableElement.

level: middleimportance: nice to knowfreq 30%

basics

~20 s

For a nested object, hand the encoder the nested type's own serializer instead of encoding fields yourself. For a field that may be null, use the nullable-element methods so null is written and read correctly instead of crashing.

open as a page

How do Json configuration flags like ignoreUnknownKeys, coerceInputValues, isLenient, and decodeEnumsCaseInsensitive affect the security/trust posture of deserializing untrusted input?

level: seniorimportance: nice to knowfreq 30%

basics

~10 s

These flags make the parser more forgiving. Forgiving is convenient but can silently accept or change attacker data. For untrusted input, prefer strict settings so unexpected data fails loudly instead of slipping through.

open as a page

What are the immutability, equality, and ordering guarantees of JsonObject and JsonArray, and what gotchas arise when you treat JsonObject as a Map (e.g. number representation)?

level: seniorimportance: nice to knowfreq 30%

basics

~20 s

JsonObject and JsonArray are immutable. JsonObject acts like a Map and keeps insertion order; equality compares contents. A common gotcha: numbers are stored as text inside JsonPrimitive, so 1 and 1.0 are different primitives and aren't equal.

open as a page

You have a sealed @Serializable Result<T> hierarchy and you also nest one sealed type as a property of another. What subtleties arise with discriminator collisions, generic type arguments, and decoding the base vs a concrete subtype?

level: seniorimportance: nice to knowfreq 25%

basics

~20 s

Polymorphic encoding only happens when you serialize through the base type, not a concrete subtype. Generic type arguments still need their own serializers, and a nested sealed property must itself be polymorphically encoded. The discriminator key must not collide with a real field name.

open as a page

How should you design and manage Json instances across an application — strictness for inbound vs outbound, reuse, and copying configuration?

level: principalimportance: nice to knowfreq 30%

basics

~20 s

Create a few shared, reusable Json instances configured for their job: strict for output, tolerant (ignoreUnknownKeys) for input. Don't build Json { } per call, and derive variants with Json(from = base) { ... }.

open as a page

You need to rename a serialized field across versions without breaking already-stored or in-flight payloads. Using kotlinx.serialization mechanics in this leaf, how do you stage that evolution safely?

level: principalimportance: nice to knowfreq 25%

basics

~10 s

Don't hard-rename. Add the new field with a default, keep accepting the old one, ignore unknown keys during transition, then drop the old field only after all old payloads are gone.

open as a page

showing 31–37 of 37