opensourceprojects.dev

A broadsheet for software that doesn't ask for your email

Effortless JSON for modern Java and Kotlin
GitHub RepoImpressions246

Project Description

View on GitHub

Moshi: Effortless JSON for Modern Java and Kotlin

If you've spent any time in the Java or Kotlin ecosystem, you've undoubtedly wrestled with JSON. Whether you're parsing API responses or building requests, converting JSON strings to objects and back again is a fundamental task. While libraries like Gson have been the old guard, they can feel a bit dated and clunky, especially when you're working in a modern Kotlin codebase. That's where Moshi from Square comes in.

Moshi is a modern JSON library that makes it easy to parse JSON into Java and Kotlin objects, and vice versa. It’s built with a focus on simplicity and performance, and it feels right at home in today's development landscape.

What It Does

In a nutshell, Moshi handles the serialization and deserialization of your objects. You give it a JSON string and a class, and it gives you a populated object. You give it an object, and it gives you a JSON string. It leverages Okio for high-performance I/O and is designed to be predictable and easy to extend when you need custom behavior.

Why It's Cool

So, what sets Moshi apart from the crowd? A few key things make it a standout choice.

First, it has first-class Kotlin support. This is a big deal. It understands Kotlin's non-nullable types and default parameter values, which many older Java-based libraries struggle with. It works seamlessly with data classes, which are the go-to for modeling JSON data in Kotlin.

Second, it's built with a powerful and intuitive adapter model. While it can handle most conversions automatically through reflection, you can easily write your own adapters for custom types or to boost performance. This system is far less magical and much more explicit than some alternatives, making debugging a breeze.

Finally, it's minimal and focused. Moshi doesn't try to be everything to everyone. It has a small API surface and a clear, singular job: convert JSON to objects and back. This makes it simple to learn and integrate without pulling in a massive dependency tree.

How to Try It

Adding Moshi to your project is straightforward. If you're using Gradle, just add the following dependencies to your build.gradle.kts file. You'll want the core library and the Kotlin-specific adapters.

// For Kotlin
dependencies {
  implementation("com.squareup.moshi:moshi:1.15.1")
  implementation("com.squareup.moshi:moshi-kotlin:1.15.1")
  kapt("com.squareup.moshi:moshi-kotlin-codegen:1.15.1") // For codegen
}

// For Java
dependencies {
  implementation("com.squareup.moshi:moshi:1.15.1")
}

Here's a quick taste of how you'd use it with a Kotlin data class. By using the moshi-kotlin-codegen artifact with kapt, you can generate the adapters at compile time for even better performance.

// Your data class
@JsonClass(generateAdapter = true)
data class User(
    val name: String,
    val username: String,
    val id: Int
)

// Parse some JSON!
val moshi = Moshi.Builder()
    .addLast(KotlinJsonAdapterFactory())
    .build()

val jsonAdapter = moshi.adapter(User::class.java)
val user = jsonAdapter.fromJson(jsonString)

You can find the full source code, detailed documentation, and more examples on the Moshi GitHub repository.

Final Thoughts

Moshi isn't necessarily trying to reinvent the wheel, but it is building a better one. It takes the lessons learned from libraries like Gson and refines the experience for modern JVM development, particularly for Kotlin. If you're starting a new project or finding your current JSON solution to be a bit cumbersome, Moshi is absolutely worth a look. It’s simple, fast, and gets out of your way—what more could you ask for?

@githubprojects

Back to Projects
Project ID: 1967557730027368846Last updated: September 15, 2025 at 11:54 AM