Generated by GPT-5-mini| Moshi (library) | |
|---|---|
| Name | Moshi |
| Author | Square, Inc. |
| Developer | Square, Inc.; community contributors |
| Latest release | 1.x |
| Programming language | Kotlin (programming language), Java (programming language) |
| Repository | GitHub |
| License | Apache License |
| Website | Square, Inc. |
Moshi (library) Moshi is a modern JSON library for Kotlin (programming language) and Java (programming language) created by Square, Inc. It provides efficient parsing, serialization, and adapters for mapping JSON to Java Virtual Machine objects used in applications such as Android (operating system), Spring Framework, and server-side Ktor (framework). Moshi emphasizes a small runtime, predictable behavior, and extensibility compatible with libraries like Retrofit (type-safe HTTP client) and OkHttp.
Moshi implements streaming and tree-style JSON processing inspired by libraries like Gson, Jackson (software), and org.json while integrating modern features from Kotlin (programming language) such as Kotlinx.serialization interop and Kotlin Reflection (kotlin.reflect). It supports annotation-driven mappings compatible with Java Annotations and Kotlin Annotation Processing Tool workflows, fitting into ecosystems including Android Jetpack, Dagger (software), Hilt (software), and Guice (software)-based applications. Moshi’s design targets projects ranging from small Android (operating system) apps to large backend systems using Spring Boot or Micronaut (framework).
Moshi offers features like reflection-free code generation via Kotlin Symbol Processing API (KSP) and annotation processors akin to AutoValue, plus runtime adapters similar to TypeAdapter concepts in Gson. It includes support for nullable types used in Kotlin (programming language), polymorphic adapters comparable to Jackson (software) modules, and customizable serialization like Jackson Databind mix-ins. Other features include JSON streaming based on JsonReader (streaming API), JSON writing with JsonWriter, lenient parsing modes used by Relaxed JSON patterns, and integration points for Okio and OkHttp for efficient I/O.
Moshi’s architecture builds on adapter chains and factories analogous to Decorator pattern usage in Java (programming language) libraries and mirrors concepts from Adapter pattern in Design patterns. Core components include the Moshi object, JsonAdapter factories, and reflection or generated code to map fields to JSON names often annotated with SerializedName-style annotations or custom annotation types inspired by Jackson Annotations. Implementation interacts with JVM bytecode produced by Kotlin (programming language) compilers and supports interoperability with Java 8 features like Optional (Java SE 8) and Streams API.
Typical usage shows creating a Moshi instance, adding adapters for types such as java.time.Instant from Java Platform, Standard Edition 8 or custom types used in Android Architecture Components, and obtaining a JsonAdapter to serialize domain models used by Model–View–ViewModel (MVVM) apps and Model–View-Presenter (MVP) apps. Common examples demonstrate integration with Retrofit (type-safe HTTP client) for REST clients consuming APIs like OpenAPI Specification endpoints, or with Ktor (framework) for server responses. Developers often combine Moshi with Coroutines from Kotlin Coroutines and dependency injection frameworks like Dagger (software) to deserialize JSON payloads in Android (operating system) or Netty servers.
Benchmarks compare Moshi against Gson, Jackson (software), Jsoniter, and Fastjson using workloads similar to those in TechEmpower benchmarks and microbenchmarks using JMH (Java Microbenchmark Harness). Moshi typically competes favorably in memory usage and startup overhead for Android (operating system) clients due to small binary size, while pure throughput can be lower than highly optimized engines like Jackson (software) in some server scenarios. Performance tuning includes using generated adapters with Kotlin Symbol Processing API or annotation processors to avoid reflection costs, and leveraging Okio buffers to reduce copying.
Moshi integrates with a wide ecosystem: network stacks like OkHttp, HTTP clients like Retrofit (type-safe HTTP client), server frameworks like Spring Boot, Ktor (framework), and Micronaut (framework). It works alongside build tools such as Gradle, Maven, and Bazel, and testing frameworks including JUnit, Mockito, and Robolectric for Android (operating system) tests. Serialization bridges exist for Kotlinx.serialization, Jackson (software), and Gson migration paths; plugins and extensions target libraries like Room (Persistence Library), WorkManager, LiveData, and observability tools like Prometheus and Grafana when tracing JSON payloads in services.
Moshi originated at Square, Inc. as part of efforts to improve JSON handling in mobile and server projects alongside libraries like OkHttp and Retrofit (type-safe HTTP client). Development progressed through community contributions on GitHub with issues and pull requests influenced by practices in Open-source software projects and guidance from standards like ECMAScript JSON semantics. Over time Moshi added features for Kotlin (programming language) support, KSP integration, and adapters for java.time types; contributors include engineers experienced with Android Open Source Project and JVM server frameworks. The project evolves through releases coordinated on GitHub Releases and discussions in ecosystems such as Android Developers and Stack Overflow.
Category:Java libraries