JSON Processing in Kotlin: Library Overview and Practical Examples
JSON (JavaScript Object Notation) remains one of the most popular formats for data exchange between client and server. In the Kotlin language, which is actively used for developing Android applications and server-side solutions, there are several powerful libraries for working with JSON. In this article, we will thoroughly examine the main tools: kotlinx.serialization, Gson, Moshi, and Klaxon. You will learn how to parse JSON, serialize objects, and efficiently process data.
1. kotlinx.serialization — Native Approach from JetBrains
kotlinx.serialization is the official library from the creators of Kotlin. Its main advantage is full integration with the language's type system and support for multiplatform (KMM). To use it, you need to add the plugin in build.gradle.kts:
plugins { kotlin("plugin.serialization") version "1.9.22"}
dependencies { implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.2")}Example of data serialization and deserialization:
import kotlinx.serialization.Serializableimport kotlinx.serialization.json.Json
@Serializabledata class User(val id: Int, val name: String, val email: String)
fun main() { val jsonString = """{"id":1,"name":"Anna","email":"anna@example.com"}""" // Deserialization val user = Json.decodeFromString<User>(jsonString) println("User: ${user.name}, email: ${user.email}") // Serialization val output = Json.encodeToString(user) println(output)}Features:
- Does not require reflection (works at compile time).
- Supports custom serializers and polymorphism.
- Excellent for Kotlin Multiplatform Mobile.
2. Gson — Classic from Google
The Gson library is known for its simplicity and widespread use. It uses reflection, making it suitable for projects where setup speed is important. Adding the dependency:
dependencies { implementation("com.google.code.gson:gson:2.10.1")}Usage example:
import com.google.gson.Gson
data class Product(val id: Int, val title: String, val price: Double)
fun main() { val gson = Gson() val json = """{"id":101,"title":"Laptop","price":79990.0}""" // Parsing val product = gson.fromJson(json, Product::class.java) println("Product: ${product.title}, price: ${product.price}") // Reverse conversion val newJson = gson.toJson(product) println(newJson)}Pros and Cons:
- + Minimal code for basic scenarios.
- + Support for
@SerializedNameannotations for field mapping. - - Using reflection can reduce performance.
- - Issues with handling
nulland generic types.
3. Moshi — Modern Alternative from Square
Moshi was created as a safer and faster replacement for Gson. It also uses reflection but provides code generation via the @JsonClass annotation. Adding the dependency:
dependencies { implementation("com.squareup.moshi:moshi-kotlin:1.15.0") ksp("com.squareup.moshi:moshi-kotlin-codegen:1.15.0")}Example usage with code generation:
import com.squareup.moshi.JsonClassimport com.squareup.moshi.Moshi
@JsonClass(generateAdapter = true)data class Book(val isbn: String, val author: String, val year: Int)
fun main() { val moshi = Moshi.Builder().build() val adapter = moshi.adapter(Book::class.java) val json = """{"isbn":"978-5-17-123456-7","autho Also in library
Working with ZIP Archives in Node.js: archiver and adm-zip Libraries
Working with Excel in TypeScript: Libraries, Examples, and Tools
Sending Email in Swift: Libraries and Examples for iOS/macOS