Test case
1 generated file · +1 −1test/inputs/json/priority/issue2680-scalar-array.json
Mkotlindefault / TopLevel.kt+1 −1
| @@ -12,6 +12,6 @@ class TopLevel(elements: Collection<Long>) : ArrayList<Long>(elements) { | ||
| 12 | 12 | public fun toJson() = klaxon.toJsonString(this) |
| 13 | 13 | |
| 14 | 14 | companion object { |
| 15 | - public fun fromJson(json: String) = TopLevel(klaxon.parseArray<Long>(json)!!) | |
| 15 | + public fun fromJson(json: String) = TopLevel(klaxon.parseArray<Any>(json)!!.map { when (it) { is Int -> it.toLong(); is Long -> it; else -> throw IllegalArgumentException() } }) | |
| 16 | 16 | } |
| 17 | 17 | } |
Test case
1 generated file · +17 −0test/inputs/schema/issue2680-top-level-array.schema
Aschema-kotlindefault / TopLevel.kt+17 −0
| @@ -0,0 +1,17 @@ | ||
| 1 | +// To parse the JSON, install Klaxon and do: | |
| 2 | +// | |
| 3 | +// val topLevel = TopLevel.fromJson(jsonString) | |
| 4 | + | |
| 5 | +package quicktype | |
| 6 | + | |
| 7 | +import com.beust.klaxon.* | |
| 8 | + | |
| 9 | +private val klaxon = Klaxon() | |
| 10 | + | |
| 11 | +class TopLevel(elements: Collection<Long>) : ArrayList<Long>(elements) { | |
| 12 | + public fun toJson() = klaxon.toJsonString(this) | |
| 13 | + | |
| 14 | + companion object { | |
| 15 | + public fun fromJson(json: String) = TopLevel(klaxon.parseArray<Any>(json)!!.map { when (it) { is Int -> it.toLong(); is Long -> it; else -> throw IllegalArgumentException() } }) | |
| 16 | + } | |
| 17 | +} |
Test case
1 generated file · +37 −0test/inputs/schema/top-level-enum.schema
Aschema-kotlindefault / TopLevel.kt+37 −0
| @@ -0,0 +1,37 @@ | ||
| 1 | +// To parse the JSON, install Klaxon and do: | |
| 2 | +// | |
| 3 | +// val topLevel = TopLevel.fromJson(jsonString) | |
| 4 | + | |
| 5 | +package quicktype | |
| 6 | + | |
| 7 | +import com.beust.klaxon.* | |
| 8 | + | |
| 9 | +private fun <T> Klaxon.convert(k: kotlin.reflect.KClass<*>, fromJson: (JsonValue) -> T, toJson: (T) -> String, isUnion: Boolean = false) = | |
| 10 | + this.converter(object: Converter { | |
| 11 | + @Suppress("UNCHECKED_CAST") | |
| 12 | + override fun toJson(value: Any) = toJson(value as T) | |
| 13 | + override fun fromJson(jv: JsonValue) = fromJson(jv) as Any | |
| 14 | + override fun canConvert(cls: Class<*>) = cls == k.java || (isUnion && cls.superclass == k.java) | |
| 15 | + }) | |
| 16 | + | |
| 17 | +private val klaxon = Klaxon() | |
| 18 | + .convert(TopLevel::class, { TopLevel.fromValue(it.string!!) }, { "\"${it.value}\"" }) | |
| 19 | + | |
| 20 | +enum class TopLevel(val value: String) { | |
| 21 | + One("one"), | |
| 22 | + Three("three"), | |
| 23 | + Two("two"); | |
| 24 | + | |
| 25 | + public fun toJson() = klaxon.toJsonString(value) | |
| 26 | + | |
| 27 | + companion object { | |
| 28 | + public fun fromJson(json: String) = fromValue(klaxon.parseArray<String>("[${json}]")!![0]) | |
| 29 | + | |
| 30 | + public fun fromValue(value: String): TopLevel = when (value) { | |
| 31 | + "one" -> One | |
| 32 | + "three" -> Three | |
| 33 | + "two" -> Two | |
| 34 | + else -> throw IllegalArgumentException() | |
| 35 | + } | |
| 36 | + } | |
| 37 | +} |
No generated files match these filters.