explaingit

square/moshi

10,131KotlinAudience · developerComplexity · 2/5Setup · easy

TLDR

Moshi is a JSON library from Square for Android, Java, and Kotlin that converts between JSON text and your app's data objects, with strong support for custom conversion logic and Kotlin null safety.

Mindmap

mindmap
  root((repo))
    What it does
      JSON parsing
      JSON serialization
      Object mapping
    Language Support
      Java reflection
      Kotlin code generation
      Android ready
    Key Features
      Custom adapters
      Null safety support
      Compile-time codegen
    Custom Adapters
      FromJson annotation
      ToJson annotation
      Field renaming
    Use Cases
      Parse API responses
      Write JSON requests
      Custom data mapping
    Tech Stack
      Kotlin
      Java
      Android
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Parse JSON responses from a REST API into Kotlin data classes in an Android app with minimal boilerplate.

USE CASE 2

Write a custom adapter to handle a JSON format where dates arrive as Unix timestamps but your class uses a Date object.

USE CASE 3

Use Moshi's code generation to skip runtime reflection and get faster, null-safe JSON parsing in a Kotlin app.

USE CASE 4

Serialize your app's data objects back to JSON to send as a request body to a web server.

Tech stack

KotlinJavaAndroid

Getting it running

Difficulty · easy Time to first run · 30min

In plain English

Moshi is a library for Android, Java, and Kotlin developers that handles reading and writing JSON data. JSON is a common text format that apps use to send and receive data, for example from a web server. Moshi converts between JSON text and the Java or Kotlin objects your code actually works with, so you do not have to manually pick apart the text yourself. The basic usage is straightforward: you point Moshi at a class you have defined, and it reads JSON into instances of that class or writes instances of that class back into JSON. It handles common data types automatically, including numbers, strings, lists, maps, and custom classes with multiple fields. Where Moshi stands out is in its support for custom adapters. If the JSON format you receive does not match your class structure directly, you write a small adapter class with methods marked @FromJson and @ToJson. Moshi calls those methods during conversion, letting you transform the data, rename fields, combine multiple fields into one, or change the format entirely. This keeps your main data classes clean while the conversion logic stays in a separate, reusable place. For Kotlin specifically, Moshi offers a code generation option that creates the adapter code at compile time rather than using reflection at runtime. This is faster and avoids some edge cases with Kotlin's null safety system. Kotlin data classes are well supported. Java reflection is also available for Java codebases. Moshi is developed and maintained by Square, the payments company, and is distributed as a dependency you add to your build file. The README provides working examples in both Java and Kotlin for all major features.

Copy-paste prompts

Prompt 1
Using Moshi in Kotlin, write the code to parse this JSON into a data class: {"id": 1, "name": "Alice", "email": "[email protected]"}.
Prompt 2
How do I write a Moshi custom adapter in Kotlin that converts a Unix timestamp integer in JSON to a Java Date object in my Android app?
Prompt 3
Show me how to add Moshi's Kotlin code generation plugin to my Gradle build and annotate a data class to use the generated adapter.
Prompt 4
What is the practical difference between Moshi with reflection versus code generation, and when should I prefer one for an Android project?
Prompt 5
How do I handle a JSON field that can be either a string or null in Moshi's Kotlin adapter without crashing?
Open on GitHub → Explain another repo

← square on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.