github.com/square/moshi

A modern JSON library for Kotlin and Java.

Open this visualization on its own page →

Contributors

79

Lines of Code

2,744

From

2014-08-09

To

2022-01-23

About square/moshi

Moshi is a modern JSON parsing and serialization library for Android, Java, and Kotlin developed by Square. It simplifies converting JSON data into Java and Kotlin objects and vice versa, with built-in support for common types like primitives, collections, strings, and enums. The library is built on top of Okio for efficient I/O operations.

The library emphasizes simplicity and safety over configurability, deliberately omitting certain features found in competing libraries like Gson. It refuses to serialize platform types without explicit user-provided adapters to prevent accidental coupling to specific JDK or Android versions. Moshi provides annotation-based customization through features like @Json for field name mapping, @JsonQualifier for type-specific encoding variants, and @ToJson/@FromJson methods for custom type adapters. It also includes convenience wrapper methods for adapters such as nullSafe(), nonNull(), and failOnUnknown().

For Kotlin users, Moshi offers two distinct approaches: a reflection-based adapter via KotlinJsonAdapterFactory and a compile-time code generation system using Kotlin Symbol Processor (KSP). The codegen approach is faster and eliminates the kotlin-reflect dependency, while reflection provides greater flexibility including support for private properties. Both methods produce identical JSON output, allowing developers to choose based on performance requirements and use case needs.

Share this video