You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
package com.example
import com.beust.klaxon.Klaxon
enum class AnEnum {
VALUE_ONE,
VALUE_TWO,
}
typealias AMap = Map<AnEnum, Int>
data class AMapContainer(val aMap: AMap)
fun main() {
val klaxon = Klaxon()
val aMap = mapOf(AnEnum.VALUE_ONE to 1, AnEnum.VALUE_TWO to 2)
val aMapContainer = AMapContainer(aMap)
println("object:\n$aMapContainer")
println("this works:")
println(aMapContainer.aMap.keys.first().toString())
val jsonString = klaxon.toJsonString(aMapContainer)
println("json string:\n$jsonString")
val recovered = klaxon.parse<AMapContainer>(jsonString)!!
println("recovered object:\n$recovered")
println("this does not work, but should:")
println(recovered.aMap.keys.first().toString())
}
Results in the following output:
object:
AMapContainer(aMap={VALUE_ONE=1, VALUE_TWO=2})
this works:
VALUE_ONE
json string:
{"aMap" : {"VALUE_ONE": 1, "VALUE_TWO": 2}}
recovered object:
AMapContainer(aMap={VALUE_ONE=1, VALUE_TWO=2})
this does not work, but should:
Exception in thread "main" java.lang.ClassCastException: class java.lang.String cannot be cast to class com.example.AnEnum (java.lang.String is in module java.base of loader 'bootstrap'; com.example.AnEnum is in unnamed module of loader 'app')
at com.example.KlaxonBugDemonstrationKt.main(klaxonBugDemonstration.kt:32)
at com.example.KlaxonBugDemonstrationKt.main(klaxonBugDemonstration.kt)
Process finished with exit code 1
I would expect the code to finish printing without error. The issue is that enums are not being deserialized back into the map keys correctly, they are coming in as strings, but the compiler thinks they are enum instances.
I am using version 5.6 btw.
The text was updated successfully, but these errors were encountered:
Running the following code:
Results in the following output:
I would expect the code to finish printing without error. The issue is that enums are not being deserialized back into the map keys correctly, they are coming in as strings, but the compiler thinks they are enum instances.
I am using version
5.6
btw.The text was updated successfully, but these errors were encountered: