Skip to content
Advertisement

Error: Class is not registered for polymorphic serialization in the scope of its interface

I need set serializable interface for using any data class in methods, example data:

@Serializable
interface Todo{}

@Serializable
data class userDataForRegistration(val name: String, val number: String, val password: String): Todo

@Serializable
data class userDataForLogin(val number: String, val password: String): Todo

@Serializable
data class contactForRemove(val id: String, val number: String): Todo

@Serializable
data class userData(val number: String)

@Serializable
data class message(val message: String)

example method, where body – some of the above data classes :

class Connection {
   val client = OkHttpClient()
   // params: login, registration, contact
   fun sendData(url: String, param: String, body: Todo){
       var json = Json.encodeToString(body)
       var reqBody = RequestBody.create("application/json; charset=utf-8".toMediaTypeOrNull(), json)

       val request = Request.Builder()
           .url(url)
           .post(reqBody)
           .build()

       client.newCall(request).enqueue(object : Callback {
           override fun onFailure(call: Call, e: IOException) {
               println("error" + e)
           }
           override fun onResponse(call: Call, response: Response){
               var res = response.body?.string()
               when(param){
                   "login", "registration" -> {
                       try{
                           val objUser = Json.decodeFromString<User>(res.toString())
                           returnUser(objUser)
                    }
                       catch(e: Exception){
                           val mes = Json.decodeFromString<message>(res.toString())
                           returnMessage(mes)
                       }
                   }   
                   "contact" ->{
                       val mes = Json.decodeFromString<message>(res.toString())
                       returnMessage(mes)
                   }
               }
           }
       })
   }

but if i calling method:

val userDataForLogin = userDataForLogin(etv_name.text.toString(), etv_pass.text.toString())
val response = connection.sendData("${ip_static.ip}/user/login", "login", userDataForLogin)

i get error:

@Serializable annotation is ignored because it is impossible to serialize automatically interfaces or enums. Provide serializer manually via e.g. companion object

I need use only TODO interface to use any data class in methods, object and abstract class will doesnt working, because it use data class

Also my plugins in build.gradle:

plugins {
id 'com.android.application' version '7.2.2' apply false
id 'com.android.library' version '7.2.2' apply false
id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
id 'org.jetbrains.kotlin.plugin.serialization' version '1.6.21'

}

I read, that kotlin.plugin.serialization 1.6.2+ working with serialization interface, but idk whats wrong with me… Thank you in advance!)

Advertisement

Answer

You should not add @Serializable to the interface, see the documentation regarding polymorphic seriliazation. Instead you must annotate the implementations and possibly register the corresponding serializers.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement