Thread
#serialization
    Nikita Khlebushkin

    Nikita Khlebushkin

    1 year ago
    Hello! I have some weird error trying to run a Ktor call with a body:
    W/System.err: kotlinx.serialization.SerializationException: Serializer for class 'Companion' is not found.
        Mark the class as @Serializable or provide the serializer explicitly.
    The place I am trying to use:
    try {
                api.createAccount(Account(handle = "some", displayname = "Some name"))
            } catch (e: Exception) {
                e.printStackTrace()
            }
    And the class itself:
    import kotlinx.serialization.Serializable
    
    @Serializable
    data class Account(val handle: String, val displayname: String)
    Ktor's HttpClient config contains:
    install(JsonFeature) {
                serializer = KotlinxSerializer(Json {
                    ignoreUnknownKeys = true
                    encodeDefaults = true
                    useAlternativeNames = false
                })
            }
    In my build.gradle.kts of the module I have:
    plugins {
        kotlin("multiplatform")
        kotlin("plugin.serialization")
        id("com.squareup.sqldelight")
        id("com.android.library")
    }
    
    kotlin {
        ...
        sourceSets {
    
            val serializationVersion = "1.2.2"
            val ktorVersion = "1.6.2"
    
    
            val commonMain by getting {
                dependencies {
                    implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:$serializationVersion")
                    implementation("io.ktor:ktor-client-core:$ktorVersion")
                    implementation("io.ktor:ktor-client-serialization:$ktorVersion")
                    implementation("io.ktor:ktor-client-logging:$ktorVersion")
                }
            }
            val androidMain by getting {
                dependencies {
                    implementation("io.ktor:ktor-client-okhttp:$ktorVersion")
                }
            }
            val iosMain by getting {
                dependencies {
                    implementation("io.ktor:ktor-client-ios:$ktorVersion")
                }
            }
        }
    }
    What am I doing wrong?
    The problem was not at serializing
    Account
    class but on deserializing the response, so the issue is closed