android-kotlin
/

Android JSON – Data Serialization and Parsing

Last Sync: Today

On this page

5
0%
5 min read
Remaining
5 minleft

Click any section to jump — progress syncs automatically

android-kotlin

Android JSON – Data Serialization and Parsing

The Evolution of JSON on Android

JSON (JavaScript Object Notation) is the universal language of mobile APIs. On Android, we transition from raw strings to structured objects. As an Engineering Manager, you must choose a serialization library that balances Performance (parsing speed) with Code Safety (nullability and type checks). Modern libraries like Kotlin Serialization are preferred because they are 'Multiplatform' ready, matching your Flutter-first mindset.

  1. Kotlin Serialization: The Native Standard

Developed by JetBrains, this is the recommended way to handle JSON in 2026. It uses a compiler plugin to generate parsing code at compile time, making it significantly faster than Gson. It also respects Kotlin's default values and null-safety, ensuring your Revochamp models never have 'missing' required fields.

KOTLINRead-only
1
// Annotate your data class for serialization
@Serializable
data class WidgetResponse(
    val id: String,
    @SerialName("widget_type") val type: String,
    val isVisible: Boolean = true // Default value used if missing in JSON
)

// Parsing a JSON string
val jsonString = """{"id": "w1", "widget_type": "Button"}"""
val widget = Json.decodeFromString<WidgetResponse>(jsonString)

  1. Moshi: The Reflection-Based Powerhouse

Moshi is the successor to Gson, built by the Square team (the creators of Retrofit). It is highly efficient and designed specifically for modern Android. It handles 'Platform Types' and JSON adapters better than almost any other library. For a Lead Developer, Moshi is the best choice when dealing with highly dynamic JSON structures from your AI backend.

  1. Handling Nested and Dynamic JSON

In Revochamp, your AI might return a list of various widget types. You can use Polymorphic Serialization to parse a list into different subclasses based on a 'type' field in the JSON. This is the native equivalent of Dart's 'switch' expressions for pattern matching in JSON mapping.

KOTLINRead-only
1
@Serializable
sealed class BaseComponent {
    @Serializable
    @SerialName("button")
    data class ButtonComponent(val text: String) : BaseComponent()

    @Serializable
    @SerialName("input")
    data class InputComponent(val hint: String) : BaseComponent()
}

JSON Library Comparison

FeatureKotlin SerializationMoshiGson (Legacy)
Primary TechCompiler PluginReflection / Code-genReflection
Null SafetyStrict (Respects Kotlin)GoodPoor (can force nulls)
SpeedVery Fast (No reflection)FastSlower
File SizeSmall (Tiny runtime)MediumMedium
ProGuard/R8Easy (No rules needed)Needs configurationComplex rules
MultiplatformYes (iOS/Web/Android)No (JVM only)No (JVM only)

Test Your Knowledge

Q1
of 3

Which annotation is required at the top of a class to use Kotlin Serialization?

A
@Json
B
@Serialized
C
@Serializable
D
@DataClass
Q2
of 3

Which library is known for being 'Multiplatform', allowing code sharing between Android and iOS?

A
Moshi
B
Gson
C
Kotlin Serialization
D
Jackson
Q3
of 3

What is the primary danger of using reflection-based JSON libraries in a production Android app?

A
They make the app icon look different
B
Performance overhead and potential for runtime null-safety violations
C
They only work on WiFi
D
They require a paid license

Frequently Asked Questions

Why should I stop using Gson?

Gson relies heavily on reflection, which is slow and can lead to runtime crashes because it doesn't understand Kotlin's null-safety. It can assign 'null' to a non-nullable Kotlin property without any warning during parsing.

What is @SerialName used for?

It is used when the JSON key name (e.g., 'user_first_name') differs from your Kotlin property name (e.g., 'firstName'). This allows you to maintain clean Kotlin coding standards while still matching the API contract.

How do I handle Date formats in JSON?

You must create a custom 'KSerializer' or 'JsonAdapter'. This tells the library how to convert a String (e.g., '2026-04-06') into a native 'java.time.LocalDateTime' object and vice versa.

Previous

android api calls

Next

android permissions

Related Content

Need help?

Explore our comprehensive docs or start a chat with our tech experts.