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
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.
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
, 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")
}
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.
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.
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'
The text was updated successfully, but these errors were encountered: