Interview frequency: …
Short answer
Because they natively support Kotlin's null safety system and do not bypass the properties' default values the way Gson does using unsafe reflection.
Explanation
Gson creates objects using an unsafe allocator without calling the constructors, which may lead to assigning null to a property declared as a non-nullable String in Kotlin, causing a sudden NullPointerException later. Moshi and Kotlinx.Serialization provide fast code generation at compile time and strict checking of types and default values.
Code example
@Serializable // Kotlinx Serialization At compile time without slow reflection
data class UserDto(
val id: Long,
val username: String,
val bio: String = "No bio" // Safely preserves the default value
)Common mistakes
- Relying on Gson with Kotlin data class objects and expecting an exception to be thrown on receiving empty fields that violate the non-null contract.