Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] "Caused by: java.lang.IllegalStateException: Only KClass supported as classifier, got T" #1860

Closed
yamatakau08 opened this issue Feb 9, 2022 · 2 comments
Labels

Comments

@yamatakau08
Copy link

Could you please teach me how to fix the the exception on the following code?
Caused by: java.lang.IllegalStateException: Only KClass supported as classifier, got T

My code is the following.

@Serializable
data class Params<T>(val params: T)        

val rparams7 = listOf(mapOf("settings" to listOf(mapOf("value" to "2", "target" to "color"))))
val params7  = Params(rparams7)
Log.d(TAG,"params7: ${Json.encodeToString(params7)}") // → params7: {"params":[{"settings":[{"value":"2","target":"color"}]}]}

This Serialization and Json.encodeToString works with no problem.

Next, I prepare the xpost func then pass the rparams7 as the raw data to that function which execute the same process
Serialization and Json.encodeToString likely the above code.

val rparams7 = listOf(mapOf("settings" to listOf(mapOf("value" to "2", "target" to "color"))))
xpost(rparams7)

fun <T> xpost(params: List<T>) {
    val x = Params(params)
    Log.d(TAG,"xpost x : $x") // -> xpost x : Params(params=[{settings=[{value=2, target=color}]}])
    val y = Json.encodeToString(x)  // Caused by: java.lang.IllegalStateException: Only KClass supported as classifier, got T
    Log.d(TAG,"xpost Json.encodeToString : $y")
    // myhttp.post(url, y)
}

But xpost Json.encodeToString has the Exception.
Caused by: java.lang.IllegalStateException: Only KClass supported as classifier, got T

Could you advice me how to fix this exception.
I just want to pass the Json.encodeToString value from xpost function to the onward function.

Environment
Android Studio Bumblebee 2021.1.1
build.gradle (Project: My_Application) classpath "org.jetbrains.kotlin:kotlin-serialization:1.6.10"
build.gradle (Module: My_Application.app) implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.2'

@qwwdfsad
Copy link
Member

This is because T is erased by JVM and we have no actual type parameters information. Unfortunately, we cannot do much except for better error message here.

As a workaround, you can make your function inline and T -- reified

@yamatakau08
Copy link
Author

Thank you for your advice.

, you can make your function inline and T -- reified

As you mentioned, other community also taught me to add
inline fun <reified T>.
this makes work successfully.

 //fun <T> xpost(params: List<T>) {
 inline fun <reified T> xpost(params: List<T>) {
     val x = Params(params)
     Log.d("HOGE","xpost x : $x") // -> in case rparams7, xpost x : Params(params=[{settings=[{value=2, target=color}]}])
     val y = Json.encodeToString(x)
     Log.d("HOGE","xpost Json.encodeToString : $y")
 } 

pdvrieze pushed a commit to pdvrieze/kotlinx.serialization that referenced this issue Apr 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants