Feedback wanted: instant Kotlin types and serializers from JSON


quicktype can now generate Kotlin types and serializers from JSON sample data.

For example, given the JSON:

  "description": {
    "title": "Contiguous U.S., Average Temperature, January-December",
    "units": "Degrees Fahrenheit",
    "base_period": "1901-2000"
  "data": {
    "189512": { "value": "50.34", "anomaly": "-1.68" },
    "189612": { "value": "51.99", "anomaly": "-0.03" }

quicktype generates:

// To parse the JSON, install Klaxon and do:
//   val temperatures = Temperatures.fromJson(jsonString)

package quicktype

import com.beust.klaxon.*

private val klaxon = Klaxon()

data class Temperatures (
    val description: Description,
    val data: Map<String, Datum>
) {
    public fun toJson() = klaxon.toJsonString(this)

    companion object {
        public fun fromJson(json: String) = klaxon.parse<Temperatures>(json)

data class Datum (
    val value: String,
    val anomaly: String

data class Description (
    val title: String,
    val units: String,

    @Json(name = "base_period")
    val basePeriod: String

We’d love feedback, suggestions, and help supporting other JSON libraries. You can find quicktype on GitHub.


I’d suggest seperating the toJson into a extension function

Temperatures.toJson() = klaxon.toJsonString

Also using a top level declaraction along with a reified type for the fromJson, so

inline fun <reified T : Any> fromJson(json: String): T = klaxon.parse<T>(json)

Like functions from the kotlin library



What should I do for the top-level fromJson when the body differs from klaxon.parse<T>(json)? For top-level arrays and maps, I need a different parse expression.


Like klaxon.parse<List<String>>(json) for this library doesn’t work?
In that case you’d make other functions like convertListFromJson(json)
Or you can make another extension function

String.convertJsonToList: List<T> {