I'm new to Kotlin and trying to parse an array of enums from JSON:
val convertColor = object: Converter<Color> {
override fun toJson(value: Color): String? = when(value) {
Color.R -> "red"
Color.G -> "green"
Color.B -> "blue"
else -> null
}
override fun fromJson(jv: JsonValue): Color = when(jv.inside) {
"red" -> Color.R
"green" -> Color.G
"blue" -> Color.B
else -> throw IllegalArgumentException("Invalid Color")
}
}
enum class Color { R, G, B }
data class Root (val colors: Array<Color>)
Then I try to parse a sample with:
val klaxon = Klaxon().converter(convertColor)
val result = klaxon.parse<Root>("""
{
"colors": ["red", "green", "blue"]
}
""")
But I get this runtime exception:
Exception in thread "main" java.lang.IllegalArgumentException: array element type mismatch
It seems like the converter is not applied to the array elements.
It works if you replace Array
with List
, which is recommended anyway:
data class Root(val colors: List<Color>)