tezvyn:

How do you configure Moshi or Kotlinx.serialization for JSON key mismatches?

AI-drafted, machine-checkedSource: github.combeginner

This tests library-specific field-mapping annotations. For Kotlinx.serialization use @SerialName with the JSON key; for Moshi use @Json with the name parameter. A red flag is confusing the two or using manual mapping instead of the built-in decorator.

WHAT THIS TESTS: This question checks whether you understand that serialization libraries decouple your Kotlin model from the wire format. The interviewer wants to see that you know the exact annotations for mapping JSON keys to Kotlin properties and that you do not confuse one library's API with another. It also surfaces whether you default to clean declarative configuration or reach for unnecessary custom code.

A GOOD ANSWER COVERS: First, state that Kotlinx.serialization uses the @SerialName annotation from the kotlinx.serialization package and that you place it directly on the property with the exact JSON string as its argument. Second, state that Moshi uses the @Json annotation from com.squareup.moshi with a name parameter set to the JSON key. Third, emphasize that the Kotlin property remains camel-cased to follow language conventions while the annotation handles the snake-case wire format. Fourth, mention that no custom adapter or serializer is needed for a simple one-to-one name mismatch because both libraries ship this built-in capability. Fifth, note that these annotations work for both deserialization and serialization so the mapping is bidirectional.

COMMON WRONG ANSWERS: A major red flag is confusing the two annotations, such as proposing @Json for Kotlinx.serialization or @SerialName for Moshi. Another weak pattern is suggesting you rename the Kotlin property to post_title to match the JSON, which abandons Kotlin naming conventions and breaks consistency across the codebase. Candidates also stumble by immediately proposing a custom JsonAdapter or KSerializer for a scenario that the library solves with a single annotation, which signals unfamiliarity with standard features. Some also forget that the annotation must be imported from the correct package and instead reference a similarly named class from a different library.

LIKELY FOLLOW-UPS: The interviewer may ask how you would handle this globally for an entire API that uses snake-case, which leads to discussing naming strategies like JsonNamingPolicy or the built-in snake-case option in Kotlinx.serialization. They might ask what happens if the JSON key is a Kotlin reserved word, which is another classic use case for these same annotations. A deeper follow-up is how you would handle a key that is absent or null and whether the annotation affects default values. They could also ask how to map nested JSON paths or multiple alternative names for the same field.

ONE CONCRETE EXAMPLE: With Kotlinx.serialization you write data class Post(@SerialName("post_title") val postTitle: String). With Moshi you write data class Post(@Json(name = "post_title") val postTitle: String). In both cases the JSON input contains the key post_title while your Kotlin code interacts with postTitle, and the library bridges the two automatically during encoding and decoding.

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.