How to Fix FastJson Parsing Errors in Kotlin Android Apps
This article analyzes why FastJson, Gson, and Kotlinx‑serialization encounter crashes or incorrect handling when parsing Kotlin data classes in Android, explains the root causes such as missing no‑arg constructors and null‑safety issues, and provides concrete code fixes, ProGuard rules, and a unified wrapper API for reliable JSON processing.
Introduction
Since Kotlin became official for Android in 2017, most projects have migrated from Java to Kotlin. However, when mixing Kotlin data classes with JSON parsing libraries such as FastJson, Gson, or Kotlinx‑serialization, developers often encounter crashes caused by reflection, missing constructors, or null‑safety issues.
Common JSON Parsing Frameworks
FastJson (originally by Alibaba, now unmaintained as FastJson1; FastJson2 is a newer fork)
Jackson (default in Spring)
Gson (Google)
Kotlinx‑serialization (official Kotlin library)
Problem Scenario
In an Android Kotlin project using FastJson 1.1.56, enabling ProGuard caused default constructor not found exceptions when parsing a JSON string into a Kotlin data class. The data class used val properties without default values, so the compiler generated only a full‑argument constructor and no set methods.
Root Cause
FastJson relies on Java reflection to locate a no‑arg constructor and setter methods. Kotlin val properties do not generate setters, and the generated constructor is not visible after obfuscation, leading to the exception.
Solution for FastJson
Change the data class to use var with default values, which forces the compiler to generate a zero‑argument constructor and setters. Add ProGuard rules to keep Kotlin reflection classes and Serializable implementations:
-keep class kotlin.reflect.jvm.** { *; }
-keep class * implements java.io.Serializable { *; }FastJson Version Status
FastJson1 is based on a 2017 release (1.1.56) and is no longer updated. FastJson2 (2.0.34.android4) was released in 2022, adds support for Android4 and Kotlin, but still requires kotlin‑reflect (≈2 MB) for data‑class handling.
Testing FastJson1 vs FastJson2
Both versions fail to parse val properties without defaults. After converting properties to var with defaults and adding the ProGuard keep rules, parsing succeeds. FastJson2 also needs the same kotlin‑reflect dependency.
Gson Behaviour
Gson can parse Kotlin data classes but assigns null to non‑nullable fields, which may cause runtime crashes. It also does not respect default values defined in the data class.
Kotlinx‑serialization
Kotlinx‑serialization generates compile‑time serializers, avoids reflection, and produces a small binary. Usage requires adding the plugin org.jetbrains.kotlin.plugin.serialization and the library kotlinx-serialization-json. Example data class annotations and serialization code are provided.
Utility Wrapper API
A set of Kotlin extension functions wraps FastJson, Gson, and kotlinx‑serialization to provide a uniform API for parseObject, toJsonString, and JSON element handling, including safe default handling and ProGuard‑friendly configurations.
Conclusion
When working with Kotlin data classes on Android, prefer kotlinx‑serialization or ensure FastJson/Gson data classes use var with defaults and proper ProGuard rules. The wrapper API simplifies migration and future upgrades.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Sohu Tech Products
A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
