Deserialize a response with Kotlinx.Serialization and Retrofit

Viewed 1795

I'm using kotlinx.serialization and implementation("com.jakewharton.retrofit:retrofit2-kotlinx-serialization-converter:0.5.0") with Retrofit2 I'm probably missing something obvious, but I'm just not putting 2 and 2 together on this one.

I have a serializable class that has a list of another serializable class:

@Serializable
data class A(
    val resultCount: Int,
    val results: List<B>
)

@Serializable
data class B(
    val x: Int,
    val y: Int,
    val z: Int
)

And retrofit setup like this:

fun provideRetrofit(): Retrofit {
    val contentType = MediaType.get("application/json")
    return Retrofit.Builder()
            .baseUrl(BaseUrl)
            .addConverterFactory(Json.asConverterFactory(contentType))
            .build()
}

I have a service:

@GET("search/")
suspend fun GetStuff(): A

When I make the network call GetStuff expecting to get an instance of A, I get this exception: kotlinx.serialization.SerializationException: Can't locate argument-less serializer for class A. For generic classes, such as lists, please provide serializer explicitly. So I guess it's expecting me to provide a serializer for the List?

I've read I should be able to use a list serializer like B.serializer().list but I'm not sure where to use that in my context, and when I try to put it anywhere I get Unresolved reference: serializer.

0 Answers
Related