Home > database >  Exception deserializing Json in Retrofit, "Polymorphic serializer was not found for missing cla
Exception deserializing Json in Retrofit, "Polymorphic serializer was not found for missing cla

Time:07-26

Working with Kotlin and Retrofit, I'm getting the following exception thrown on response to my http post, before onResponse()/onFailure() can get called:

kotlinx.serialization.json.internal.JsonDecodingException: Polymorphic serializer was not found for missing class discriminator ('null') JSON input: {"CustID":101,"DeviceID":81}

I'm just using a simple data class though, no polymorphism. What am I missing here?

My data object (I've added a lot of decoration trying to debug this):

@SerialName("NewCustVals")
@Serializable
data class NewCustVals (
    @SerialName("CustID")
    var CustID: Int? = 0,
    @SerialName("DeviceID")
    var DeviceID: Int? = 0
)

My service:

interface LTECustomerInterface {
    companion object {
        private var TAG :String = "LTECustomerInterface"
        var BASE_URL = "http://XXXXXXXXXXX:XXXX/"  //test server
        lateinit var callbackApi :ApiCallback

        @OptIn(ExperimentalSerializationApi::class)
        fun create(cb:ApiCallback): LTECustomerInterface {

            callbackApi = cb

            val retrofit = Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(Json{ignoreUnknownKeys = true; isLenient = true}.asConverterFactory( "application/json".toMediaType()))
             //   .client(client)
                .build()

            return retrofit.create(LTECustomerInterface::class.java)
        }
    }

    @POST("/newCustomer")
    suspend fun newCustomer(@Body data : Customer ) :Call<NewCustVals>
}

The call:

fun sendCustomer() {
    viewModelScope.launch(Dispatchers.IO) {
       val call = serverCustomerHttpInterface.newCustomer(_currentCust.value)

           call.enqueue(object : retrofit2.Callback<NewCustVals>{
             override fun onResponse(call: Call<NewCustVals>, response: Response<NewCustVals>) {
               Log.i(TAG,"NewCustomer onResponse called")
             }

             override fun onFailure(call: Call<NewCustVals>, t: Throwable) {
               Log.i("sendCustomer()","NewCustomer onFailure")
             }
          })
    }
}

Reasonably new to Retrofit/Kotlin, I'm guessing I've missed something pretty obvious. Any ideas would be very appreciated.

Thanks

CodePudding user response:

Could you change the return type from call to respond and make the necessary changes and check..

"Call" is useful when we are willing to use its enqueue callback function.

When we use Coroutines or RxJava in the project(which is the best professional practice) to provide asynchronous execution , we don't need enqueue callback. We could just use Response.

Since we are not using the callback the polymorphism is not triggered.

github gist for kotlin serialization issue

  • Related