Gson vs Moshi vs kotlinx.serialization: Kotlin JSON parsing compared
A technical breakdown compares three popular Kotlin JSON libraries — Gson, Moshi, and kotlinx.serialization — across key behaviors like null-safety, default values, and multiplatform support. Gson, the oldest of the three, silently violates Kotlin null-safety by writing null into non-nullable fields without throwing an error, making it a risky choice for modern Kotlin code. Moshi with codegen enforces nullability at parse time and handles default values correctly, making it a practical fit for Android projects using OkHttp or Retrofit. kotlinx.serialization uses a compiler plugin instead of reflection, supports multiplatform targets including JS and native, but requires explicit configuration to ignore unknown JSON keys. The consensus is to avoid Gson for new projects, use Moshi for existing Android stacks, and prefer kotlinx.serialization for fresh or multiplatform builds.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in