A modern JSON library for Android and Java by Square for efficient parsing and serialization.
Quick Demo
Add implementation 'com.squareup.moshi:moshi:1.15.0' to your app's build.gradle file and sync.@JsonClass(generateAdapter = true)
data class Product(val id: Int, val name: String)val moshi = Moshi.Builder().build()
val adapter = moshi.adapter(Product::class.java)
val product = adapter.fromJson('{"id": 1, "name": "Sample"}')Run the app on an Android device or emulator to verify the parsed object in logs or UI.
Quick Start
Setup Steps
- Add the Moshi dependency to your build.gradle file.
- Sync your project with Gradle.
- Define data classes with Moshi annotations for your JSON models.
- Use Moshi's adapters to parse JSON strings into objects and vice versa.
Overview
Moshi is a modern JSON library for Android and Java that simplifies parsing JSON into Java objects and serializing objects back to JSON. Built on Okio, it offers high performance, type safety, and excellent Kotlin integration with code generation support.
Features
Use Cases
When Not to Use
Usage Examples
val moshi = Moshi.Builder().build()
val jsonAdapter = moshi.adapter(User::class.java)
val user = jsonAdapter.fromJson(jsonString)Parse a JSON string into a User object using Moshi.
@JsonClass(generateAdapter = true)
data class User(val name: String, val age: Int)Define a Kotlin data class with Moshi annotation for automatic adapter generation.
class DateAdapter {
@ToJson fun toJson(date: Date): String = date.toString()
@FromJson fun fromJson(dateString: String): Date = Date(dateString)
}Create a custom JsonAdapter to handle Date objects in JSON.
Common Pitfalls
- Forgetting to enable Kotlin codegen plugin in build.gradle
- Mixing up JsonAdapter types for polymorphic data
- Not handling nullability correctly in Kotlin, leading to runtime errors
- Overusing reflection when code generation is available for better performance
Integration
Works well with
- Moshi can be used as a converter with Retrofit for seamless JSON handling in network calls.
- Ensure to add necessary dependencies like moshi-kotlin for Kotlin support.
Alternatives
Gson
Reflection-based, easier setup but less type-safe and slower for large data.
Jackson
Feature-rich with extensive modules, but has a larger footprint and steeper learning curve.
Kotlinx.serialization
Native Kotlin serialization, ideal for Kotlin-only projects but less Java interoperability.
FAQ
What is the main advantage of Moshi over Gson?
Moshi uses code generation for better type safety and performance, while Gson relies on reflection, which can be slower and less secure.
Does Moshi support Kotlin data classes?
Yes, Moshi has excellent Kotlin support, including annotations for automatic adapter generation with data classes.
How do I handle custom JSON formats in Moshi?
You can write custom JsonAdapters to serialize and deserialize non-standard or complex JSON structures.
Is Moshi compatible with Android projects?
Yes, Moshi is designed to work efficiently on Android and is widely adopted in Android app development.
What dependencies are required for Moshi?
Moshi core depends on Okio; for Kotlin, you may need additional modules like moshi-kotlin and the Kotlin codegen plugin.
Maintenance & Health
- Exact latest version and Maven coordinates are not specified; check the official repository for updates.
- Min SDK, compile SDK, and target SDK values depend on the Android project setup and are not defined by Moshi directly.
- Logo URL and website URL are set to null due to uncertainty about official sources.