Anil Sai
Anil Sai
Android Developer

📡Android different serialization techniques

📡Android different serialization techniques

🥊 Gson vs Moshi vs kotlinx.serialization: The Ultimate JSON Showdown in Android

In Android development, JSON parsing is an everyday task. Whether you’re fetching API data or persisting state, choosing the right serialization library is crucial.

The three most popular contenders are:

  • Gson: The old-school favorite by Google
  • Moshi: A modern, reflection-free solution by Square
  • kotlinx.serialization: Kotlin’s own type-safe, multiplatform-first library

Let’s compare them in terms of performance, syntax, flexibility, and use cases.


🧬 TL;DR Comparison Table

Feature Gson Moshi kotlinx.serialization
Kotlin-first ⚠️ Partial
Reflection-free ✅ (with adapters)
Annotation style Java-style Cleaner Kotlin Native Kotlin
Default value support
Custom serializers
Built-in null safety
Multiplatform support
Retrofit integration ⚠️ (requires converter)
Performance Moderate High Very High

🧪 1. Gson

Developed by: Google
Maturity: Very mature, long-standing
Reflection-based: Yes (uses java.lang.reflect)
Pros:

  • Simple API
  • Widely used, well-documented
  • Supported out-of-the-box in Retrofit

Cons:

  • Slower due to reflection
  • No native Kotlin support (no default values, null safety)
  • Can’t handle Kotlin-specific features like val/var well

Example

1
2
3
4
data class User(val name: String, val age: Int)
val gson = Gson()
val json = gson.toJson(User("Alice", 25))
val user = gson.fromJson(json, User::class.java)

⚡ 2. Moshi Developed by: Square Reflection-free: Yes (when using KotlinJsonAdapterFactory) Kotlin Support: Good (but not native)

Pros:

No reflection if you use code-gen

Supports custom adapters and annotations

Works great with Retrofit

Safer handling of nulls and default values

Cons:

Kotlin support requires kapt/code-gen setup

More boilerplate for custom adapters

Example

1
2
3
4
5
6
7
8
9
10
@JsonClass(generateAdapter = true)
data class User(val name: String, val age: Int)

val moshi = Moshi.Builder()
    .add(KotlinJsonAdapterFactory())
    .build()

val adapter = moshi.adapter(User::class.java)
val json = adapter.toJson(User("Alice", 25))
val user = adapter.fromJson(json)

🌐 3. kotlinx.serialization

Developed by: JetBrains Kotlin-native: ✅ Reflection-free: ✅ Multiplatform support: ✅

Pros:

Compile-time safety

Native Kotlin syntax

Fast and lightweight

Works in Kotlin Multiplatform (JVM, JS, Native)

Great for modern Android development

Cons:

Retrofit doesn’t support it out-of-the-box

Custom serialization can be verbose

Example kotlin Copy Edit @Serializable data class User(val name: String, val age: Int)

val json = Json.encodeToString(User(“Alice”, 25)) val user = Json.decodeFromString(json) 🧩 Use Cases

Use Case Recommended Library Legacy projects with Gson ✅ Stick with Gson New Android app with Retrofit ✅ Moshi or kotlinx.serialization Kotlin Multiplatform (KMM) ✅ kotlinx.serialization Performance-critical apps ✅ kotlinx.serialization Need for full JSON flexibility ✅ Moshi 🔄 Retrofit Integration

Library Retrofit Support Gson ✅ Native Moshi ✅ Native kotlinx.serialization ⚠️ Via retrofit-kotlinx-serialization-converter ✅ Summary Use Gson if you’re maintaining a legacy codebase or need simplicity.

Choose Moshi for most Android apps — it’s fast, safe, and works great with Retrofit.

Go with kotlinx.serialization if:

You’re building with Kotlin Multiplatform

You want full compile-time safety

You value performance and Kotlin idioms

🧠 Final Thoughts Choosing the right serialization tool depends on your project goals, platform requirements, and team preferences. While Gson still works, Moshi and kotlinx.serialization are the future — especially in modern Android and Kotlin-first environments.

comments powered by Disqus