https://kotlinlang.org logo
Title
c

chanjungskim

02/27/2023, 5:57 AM
I have this error. When using Retrofit2
Caused by: kotlinx.serialization.SerializationException: Serializer for class 'Result' is not found.
    Mark the class as @Serializable or provide the serializer explicitly.
        at kotlinx.serialization.internal.PlatformKt.serializerNotRegistered(Platform.kt:32)
        at kotlinx.serialization.SerializersKt__SerializersJvmKt.serializer(SerializersJvm.kt:60)
        at kotlinx.serialization.SerializersKt.serializer(Unknown Source:1)
        at com.jakewharton.retrofit2.converter.kotlinx.serialization.Serializer.serializer(Serializer.kt:24)
        at com.jakewharton.retrofit2.converter.kotlinx.serialization.Factory.responseBodyConverter(Factory.kt:28)
        at retrofit2.Retrofit.nextResponseBodyConverter(Retrofit.java:362)
        at retrofit2.Retrofit.responseBodyConverter(Retrofit.java:345)
        at retrofit2.HttpServiceMethod.createResponseConverter(HttpServiceMethod.java:124)
        	... 90 more
Is there any problem?...
val serializationJson = Json {
            useAlternativeNames = true
            isLenient = true
            ignoreUnknownKeys = true
        }

        return Retrofit.Builder()
            .baseUrl(baseUrl)
            .client(client.build())
            .addCallAdapterFactory(CoroutineCallAdapterFactory())
            .addConverterFactory(ScalarsConverterFactory.create())
            .addConverterFactory(serializationJson.asConverterFactory("application/json".toMediaType()))
            .build()
c

Chrimaeon

02/27/2023, 8:20 AM
I think the issue is not in the setup but how you use the serializer. How did you configure the
Result
class? Did you add the Annotation as spotted in the error report?
c

chanjungskim

02/27/2023, 8:22 AM
yes. Here's the data class.
import kotlinx.serialization.Serializable

@Serializable
data class ManagerLoginResponseBody(
    val id: Int,
    val kiosk: String, // will be removed
    val created: String? = null,
    val modified: String? = null,
    val name: String,
    val mobile: String,
    val role: String,
    val branch: BranchModel,
    val token: String
)
c

Chrimaeon

02/27/2023, 8:23 AM
But that’s not the
Result
class Form the error.
c

chanjungskim

02/27/2023, 8:23 AM
the Result class is from kotlin library
@SinceKotlin("1.3")
@JvmInline
public value class Result<out T> @PublishedApi internal constructor(
    @PublishedApi
    internal val value: Any?
) : Serializable {
    // 
}
c

Chrimaeon

02/27/2023, 8:24 AM
If you use it, you’ll need to create a serializer for it.
c

chanjungskim

02/27/2023, 8:27 AM
isn't this part?
.addConverterFactory(serializationJson.asConverterFactory("application/json".toMediaType()))
c

Chrimaeon

02/27/2023, 8:27 AM
No
c

chanjungskim

02/27/2023, 8:28 AM
How can I create a serializer ??
Json.encodeToString
this one..?
c

Chrimaeon

02/27/2023, 3:23 PM
Please check the documentation of the Kotlinx serializer plug-in. There are plenty of examples how to create a serializer.