quicktype can now generate Kotlin types and serializers from JSON sample data. Here's a quick demo showing how easy it is to get started with Kotlin and quicktype.
Why Kotlin?
Kotlin has become the preferred language for Android development and is increasingly used for backend services. With quicktype's Kotlin support, you can:
- Generate data classes from JSON
- Get serialization code via kotlinx.serialization or Klaxon
- Use the same JSON with Android, iOS (via Kotlin/Native), and backend services
Example
Given this JSON:
{
"name": "Pikachu",
"type": ["Electric"],
"stats": {
"hp": 35,
"attack": 55,
"defense": 40
}
}
quicktype generates:
data class Pokemon(
val name: String,
val type: List<String>,
val stats: Stats
)
data class Stats(
val hp: Long,
val attack: Long,
val defense: Long
)
With Klaxon Serialization
quicktype pokemon.json -l kotlin --framework klaxon -o Pokemon.kt
This adds Klaxon annotations and helper methods for JSON parsing:
private val klaxon = Klaxon()
data class Pokemon(
val name: String,
val type: List<String>,
val stats: Stats
) {
fun toJson() = klaxon.toJsonString(this)
companion object {
fun fromJson(json: String) = klaxon.parse<Pokemon>(json)
}
}
Try It
Visit app.quicktype.io and select Kotlin, or use the CLI:
npm install -g quicktype
quicktype --lang kotlin your-data.json -o Types.kt