A modern JSON library for Android and Java by Square for efficient parsing and serialization.

Apache 2.0

Quick Demo

1Add Dependencyterminal
Add implementation 'com.squareup.moshi:moshi:1.15.0' to your app's build.gradle file and sync.
2Create Data Modelcode
@JsonClass(generateAdapter = true)
data class Product(val id: Int, val name: String)
3Parse JSON Responsecode
val moshi = Moshi.Builder().build()
val adapter = moshi.adapter(Product::class.java)
val product = adapter.fromJson('{"id": 1, "name": "Sample"}')
4Run on Androidphone

Run the app on an Android device or emulator to verify the parsed object in logs or UI.

Quick Start

Setup Steps

  1. Add the Moshi dependency to your build.gradle file.
  2. Sync your project with Gradle.
  3. Define data classes with Moshi annotations for your JSON models.
  4. 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

Type-safe JSON parsing with code generation
First-class Kotlin support including data classes
Custom JsonAdapters for complex or non-standard JSON
Efficient I/O built on Okio
Lightweight and minimal dependencies

Use Cases

Parsing API responses in Android applications
Serializing objects for local storage or caching
Configuring JSON data for network requests with libraries like Retrofit
Handling configuration files in JSON format

When Not to Use

When you need XML parsing or other data formats
For extremely simple JSON manipulation without defined models
If you prefer reflection-based libraries like Gson for rapid prototyping
In environments where code generation is not feasible

Usage Examples

Basic JSON Parsing
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.

Kotlin Data Class with Annotations
@JsonClass(generateAdapter = true)
data class User(val name: String, val age: Int)

Define a Kotlin data class with Moshi annotation for automatic adapter generation.

Custom Adapter Example
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

RetrofitOkHttpRoomKotlin coroutines
  • 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.

Related libraries