Moshi for Kotlin: Complete Guide with Examples
\n\n1. What It Is and Why You Need It
\nMoshi is a modern library for JSON serialization and deserialization, developed by Square. It is written in Kotlin and Java, but is primarily aimed at Kotlin developers. Moshi provides a powerful, safe, and performant way to convert objects to and from JSON, with minimal boilerplate code.
\nWhy do you need Moshi? In modern application development, working with JSON is an integral part: data exchange with REST APIs, storing configurations, passing messages between components. Moshi solves several key tasks: automatic conversion of Kotlin classes to JSON (and vice versa) without the need to write parsers manually; strong typing, which prevents runtime errors; support for immutable objects (data class) and sealed classes; the ability to customize via adapters. Compared to Gson or Jackson, Moshi offers a cleaner API, better integration with Kotlin (e.g., support for null-safety and default values), and a smaller library size.
\nMoshi is especially useful in projects where performance and type safety are important. It is actively used in Android development (including in combination with Retrofit and OkHttp), in server applications on Ktor, and in any Kotlin projects that require working with JSON. The library supports reflection (via the moshi-kotlin module) and code generation (via moshi-kotlin-codegen), allowing you to choose between convenience and performance.
2. Installation
\nMoshi is distributed via Maven Central. To install, add dependencies to the build.gradle.kts (Gradle) or pom.xml (Maven) file.
Gradle (Kotlin DSL)
\n// build.gradle.kts\ndependencies {\n // Core library\n implementation("com.squareup.moshi:moshi:1.15.1")\n \n // Kotlin module (reflection)\n implementation("com.squareup.moshi:moshi-kotlin:1.15.1")\n \n // Code generation (optional, for improved performance)\n ksp("com.squareup.moshi:moshi-kotlin-codegen:1.15.1")\n // or kapt("com.squareup.moshi:moshi-kotlin-codegen:1.15.1")\n}\n\n Gradle (Groovy)
\n// build.gradle\ndependencies {\n implementation 'com.squareup.moshi:moshi:1.15.1'\n implementation 'com.squareup.moshi:moshi-kotlin:1.15.1'\n ksp 'com.squareup.moshi:moshi-kotlin-codegen:1.15.1'\n}\n\n Maven
\n<dependency>\n <groupId>com.squareup.moshi</groupId>\n <artifactId>moshi</artifactId>\n <version>1.15.1</version>\n</dependency>\n<dependency>\n <groupId>com.squareup.moshi</groupId>\n <artifactId>moshi-kotlin</artifactId>\n <version>1.15.1</version>\n</dependency>\n\n Important: If you use code generation, do not forget to add the KSP (Kotlin Symbol Processing) or KAPT plugin. For KSP, add to plugins:
plugins {\n id("com.google.devtools.ksp") version "1.9.22-1.0.17"\n}\n 3. Quick Start
\nA minimal working example demonstrates basic serialization and deserialization.
\n \nimport com.squareup.moshi.Moshi\nimport com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory\n\n// 1. Define a data class\ndata class User(\n val name: String,\n val age: Int,\n val email: String?\n)\n\nfun main() {\n //