2

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.

tcox
  • 126
  • 2
  • 6
  • can you please edit your question and print build.gradle (Project level) it seems that plugin is not properly applied. – Parth Apr 12 '20 at 04:58
  • Can you try this line `.addConverterFactory(Json(JsonConfiguration(strictMode = false)).asConverterFactory(contentType))` in your `provideRetrofit` method and let me know if you still face the error – Raghul Vaikundam Apr 12 '20 at 08:29
  • @Parth You're right, I just didn't add the dependency to my project and didn't apply the plugin. Applying the plugin fixed the issue. Thank you! – tcox Apr 26 '20 at 04:16

0 Answers0