klaxon icon indicating copy to clipboard operation
klaxon copied to clipboard

Could not parse data for class with generic field

Open onebone opened this issue 6 years ago • 5 comments

Klaxon couldn't parse data for class with generic field.

data class A (val a: Int)
data class Parse<T>(val a: T)

Klaxon().parse<Parse<A>>("""{"a":{"a":1}}""")

causes

java.lang.ClassCastException: kotlin.reflect.jvm.internal.KTypeParameterImpl cannot be cast to kotlin.reflect.KClass
        at com.beust.klaxon.Annotations$Companion.findJsonPaths(Annotations.kt:63)
        at com.beust.klaxon.Annotations$Companion.findJsonPaths(Annotations.kt:56)
        at com.beust.klaxon.Klaxon.parser(Klaxon.kt:141)
        at com.beust.klaxon.Klaxon.parser$default(Klaxon.kt:140)
       ...

Tested with Klaxon 3.0.5.

onebone avatar Jun 03 '18 03:06 onebone

Any update here? Having similar issue

vitalybaev avatar Aug 25 '18 14:08 vitalybaev

+1

raderio avatar Dec 21 '18 13:12 raderio

Looking into this.

Right now I'm struggling trying to recover the fact that T's effective type is A, and I can't seem to be able to get this from kotlin-reflect so far.

cbeust avatar Dec 21 '18 16:12 cbeust

Any news? Or maybe workaround ideas? I tried to do something wiht TypeAdapter, but lost the battle with Kotlin :(

Equidamoid avatar Jan 20 '19 13:01 Equidamoid

It seems its cause is type erasure which is the feature of JVM. It may need some different approach to resolve this issue.

onebone avatar Jan 20 '19 14:01 onebone