Open Mitti30 opened 3 years ago
Can you please add the Json you're trying to deserialize? It looks like it does not have 'type' field
It's not having the type field, thats why I give him the type with the function call.
@POST("/")
suspend fun initDevice(@Body body:SimpleRequest):SimpleResponse<InitResponse>
Ok, now i added
class ResultSerializer<T>(private val dataSerializer: KSerializer<T>): KSerializer<Success<T>>{
override val descriptor: SerialDescriptor=dataSerializer.descriptor
override fun deserialize(decoder: Decoder): Success<T> = Success(dataSerializer.deserialize(decoder))
override fun serialize(encoder: Encoder, value: Success<T>) =dataSerializer.serialize(encoder,value.data)
}
but im stuck with the same bug as shown in this issue ^^
Hey @sandwwraith, I'm working with @Mitti30 on this problem in our app. As he described, we have two cases:
success case: the request was handeled successful and returns json in the following format. The content of Data
can change depending on the request type, therefore we use Success
to wrap the actual type like InitResponse
. So for each API endpoints, we have a response type Success<ResponseType>
:
{
"Data":{
"someID":0,
"anotherProperty":[
"valueA",
"valueB"
]
}
}
error case: something went wrong with the request, e.g. wrong username/password, resource not found or anything like that. In this case the server returns json like this:
{
"error":{
"message":"Username or password invalid",
"level":"DEBUG",
"code":401,
}
}
We now want to define a deserializer that can be used by retrofit to either return the Success<T>
or Error
object. As the json comes directly from our server, there is no type
field provided. So we can not infer the type by looking at the json. Instead, it is only known from the type of the retrofit call as @Mitti30 showed above. I hope it is a bit easier to understand now.
I see the problem. The good approach for it is to remove polymorphic serialization and write your own custom serializer, like in this comment: https://github.com/Kotlin/kotlinx.serialization/issues/1313#issuecomment-770994374 . However, due to the mentioned kapt issue, it's impossible for now. Probably, a viable workaround can be to move models and serializers to the separate Gradle module, which is not processed by kapt.
I have to work with an API that responds with JSON:
{ "Data":T }
{ "error":{ "msg":String, "id":Int } }
so i have built the corresponding classes.
the generic in Success can be something like this:
i defined the SerializationModule like this:
But its throwing me
Did i forgot to define something?