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