Home > Enterprise >  kotlinx.serialization.json.internal.JsonDecodingException: Polymorphic serializer was not found for
kotlinx.serialization.json.internal.JsonDecodingException: Polymorphic serializer was not found for

Time:09-12

I'm relatively new to Kotlin and use Ktor to do networking inside my android app. I wanted to decode and parse an endpoint so I created a data class

@Serializable
data class ProjectDashboardResponse(
    @SerialName("projects")
    val projects: List<String>?,
    @SerialName("total_count")
    val total_count: Int?,
    @SerialName("offset")
    val offset: Int?,
    @SerialName("limit")
    val limit: Int?

)

but upon calling the function to getTheData which is

   override suspend fun getProjectDashboardData(): List<ProjectDashboardResponse> {
        return client.get {
            url {
                protocol = URLProtocol.HTTPS
                host = "example.com"
                path("/projects/listofsomething.json")
            }
            headers {
                append(HttpHeaders.Accept, "text/html")
                append(HttpHeaders.Authorization, "Basic ${"${projectDashboardViewModel.userName}:${projectDashboardViewModel.password}".encodeBase64()}")
            }
        }.body()

    }

I get the following error log shown

Suppressed: kotlinx.coroutines.DiagnosticCoroutineContextException: [androidx.compose.runtime.BroadcastFrameClock@953c390, StandaloneCoroutine{Cancelling}@fa05489, AndroidUiDispatcher@bfbec8e]
    Caused by: kotlinx.serialization.json.internal.JsonDecodingException: Polymorphic serializer was not found for missing class discriminator ('null')
    JSON input: {"projects":[],"total_count":0,"offset":0,"limit":25}

Can someone point out where I am going wrong, can't find nothing in the docs. Thanks

CodePudding user response:

I solved it, for anyone else wondering on encountering the same problem I did. The response I've got was an object although I tried to parse it as an array. So instead of using items {} I managed to use the response with item {}

  • Related