kotlinx.serialization

kotlinx.serialization and @Serializable Data Classes

kotlinx.serialization 5,947 (github.com/Kotlin/kotlinx.serialization (https://github.com/Kotlin/kotlinx.serialization 5,947 ), Apache 2.0, 1.11.0) differs from Gson 24,235 's reflection: a compiler plugin (already applied for Type-Safe Routes's routes) generates a serializer per @Serializable class at build time, aware of nullability and defaults, on every Kotlin platform. BookNest keeps the wire format in its own class:

data/remote/BookApi.kt: the server's JSON as a data class (excerpt)Kotlin
@Serializable
data class BookDto(
  val id: Int,
  val title: String,
  ...
  val inStock: Boolean = true,                     // a missing key takes the default
  val summary: String = "",
  @SerialName("cover_url") val coverUrl: String? = null,
)

@SerialName maps the server's snake_case key, and defaults make keys optional. The server also sends an unmodeled updatedAt field. A unit test decodes one book both ways:

Output of 155
BookDtoTest > lenientJsonUsesDefaults STANDARD_OUT
    lenient: BookDto(id=3, title=Salt and Saffron, author=Priya Nair, genre=Cooking,
price=21.6, rating=4.8, pages=256, year=2023, inStock=true, summary=, coverUrl=/covers/3.png)
BookDtoTest > strictJsonRejectsUnknownKeys STANDARD_OUT
    strict: Unexpected JSON token at offset 164: Encountered an unknown key 'updatedAt'
at path: $

The default Json is strict, so one new server field would break every installed copy of the app. Configure Json { ignoreUnknownKeys = true } once and share it (NetworkModule provides it as a @Singleton; the plugin even warned about the test's inline Json { }: "Redundant creation of Json format"). Keep DTOs separate from entities and UI models, as BookDto.toEntity() does, so a server rename touches one mapper.