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.