Implementing JSON Serialization and Deserialization in Kotlin Data Classes Using GSON

Dec 04, 2025 · Programming · 9 views · 7.8

Keywords: Kotlin | Data Class | GSON | JSON Serialization | @SerializedName

Abstract: This article provides an in-depth exploration of using the GSON library for JSON serialization and deserialization with Kotlin data classes. By comparing the differences between Java POJO classes and Kotlin data classes, it focuses on the application of the @SerializedName annotation in Kotlin, including how to specify JSON key names for data class properties. Complete code examples demonstrate the conversion process from JSON strings to Kotlin objects and the generation of JSON strings from Kotlin objects. The advantages of Kotlin data classes in JSON processing are also discussed, such as concise syntax and automatically generated equals(), hashCode(), and toString() methods.

Fundamentals of Kotlin Data Classes and JSON Serialization

In modern Android and server-side development, JSON (JavaScript Object Notation) has become the de facto standard format for data exchange. Kotlin, as a modern programming language, offers data classes that provide a more concise and type-safe approach to handling JSON data. Compared to traditional Java POJO (Plain Old Java Object) classes, Kotlin data classes significantly improve development efficiency by reducing boilerplate code.

Application of @SerializedName Annotation in Kotlin

GSON is a popular Java library developed by Google for serializing Java objects to JSON format and deserializing JSON strings to Java objects. In Kotlin, we can use the GSON library directly, and the usage of the @SerializedName annotation is essentially the same as in Java. This annotation allows us to specify the mapping between JSON key names and class property names, which is particularly useful when JSON key names do not conform to Kotlin naming conventions or need to remain consistent with backend APIs.

The following is a complete Kotlin data class example demonstrating how to use the @SerializedName annotation:

data class Topic(
  @SerializedName("id") val id: Long, 
  @SerializedName("name") val name: String, 
  @SerializedName("image") val image: String,
  @SerializedName("description") val description: String
)

In this example, we define a data class named Topic with four properties: id, name, image, and description. Each property uses the @SerializedName annotation to specify the corresponding JSON key name. This declaration ensures that GSON can correctly perform serialization and deserialization even when JSON key names do not exactly match class property names.

JSON Serialization Implementation

Converting a Kotlin object to a JSON string is a straightforward process. First, create a Gson instance, then call its toJson() method:

val gson = Gson()
val json = gson.toJson(topic)

Here, topic is an instance of the Topic class. The toJson() method iterates through all properties of the topic object, generating corresponding JSON key-value pairs based on the key names specified by the @SerializedName annotations. If no @SerializedName annotation is provided, GSON defaults to using the property name as the JSON key name.

JSON Deserialization Implementation

Creating a Kotlin object from a JSON string is equally simple. First, obtain the JSON string, then use Gson's fromJson() method:

val json = getJson()
val topic = gson.fromJson(json, Topic::class.java)

In this example, the getJson() function returns a JSON string containing Topic data. The fromJson() method parses this string and creates the corresponding Kotlin object based on the definition of the Topic class (including the @SerializedName annotations). Note that we use Topic::class.java to obtain the Java Class object for the Topic class, which is a requirement of the GSON API.

Advantages of Kotlin Data Classes

Kotlin data classes offer several significant advantages when handling JSON data:

  1. Conciseness: Data classes automatically generate equals(), hashCode(), toString(), and copy() methods, reducing boilerplate code.
  2. Immutability: Properties declared with the val keyword are immutable, which helps create thread-safe code.
  3. Null Safety: Kotlin's type system distinguishes between nullable and non-nullable types, allowing null pointer exceptions to be caught at compile time.
  4. Default Parameters: Kotlin supports default parameter values, which is particularly useful when dealing with potentially missing JSON fields.

Practical Considerations

In actual development, additional factors should be considered:

By appropriately using Kotlin data classes and the GSON library, developers can handle JSON data in a type-safe and efficient manner while maintaining code conciseness and maintainability. This combination is particularly suitable for modern Android applications and backend services, where RESTful API communication is a core functionality.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.