Ktor: Serializer for class 'JsonLiteral' is not found

Recently updated my Ktor from 1.6 to 2.3.0 and some requests work fine but others have this error:

Serializer for class ‘JsonLiteral’ is not found.
Please ensure that class is marked as ‘@Serializable’ and that the serialization compiler plugin is applied.

It works fine when I switch back to 1.6 and nothing was changed in the data classes. I use kotlinx.serialization.

Ktor

import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.engine.cio.*
import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.client.plugins.logging.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.serialization.kotlinx.json.*
import kotlinx.coroutines.*
import kotlinx.serialization.json.Json

    val httpClient: HttpClient = HttpClient(CIO) {
        expectSuccess = false
        install(ContentNegotiation) {
            json(Json {
                encodeDefaults = false
                ignoreUnknownKeys = true
                isLenient = true
                useAlternativeNames = false
            })
        }
        install(Logging) {...}
    }

build.gradle.kts

plugins {
    val kotlinVersion = "1.8.21"
    kotlin("jvm") version kotlinVersion
    kotlin("plugin.serialization") version kotlinVersion
//    id("org.jetbrains.kotlin.plugin.serialization") version kotlinVersion
    application
}

dependencies {
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlinVersion")
    implementation(kotlin("reflect"))
    testImplementation(kotlin("test"))

    // Ktor client
    implementation("io.ktor:ktor-client-core:$ktorVersion")
    implementation("io.ktor:ktor-client-cio:$ktorVersion")
    implementation("io.ktor:ktor-client-json:$ktorVersion")
//    implementation("io.ktor:ktor-client-serialization:$ktorVersion")
    implementation("io.ktor:ktor-serialization-kotlinx-json:$ktorVersion")
    implementation("io.ktor:ktor-client-content-negotiation:$ktorVersion")
    implementation("io.ktor:ktor-client-logging:$ktorVersion")

What is this JsonLiteral I’m hiddenly using? It’s marked as deprecated. Looks like some imports need to be upgraded or something inobvious.

It looks like something wrong with body in the POST requests (GETs are fine).

@Test
fun sendPost() = runBlocking{
    val requestData = "{\"search\":{\"dates\":\"current\"}}"
    val json = Json.parseToJsonElement(requestData)
    val result = Ktor.postAsync<HttpResponse>("http://127.0.0.1:8080", requestHeaders, json)
    assertEquals("", result.toString())
}

suspend inline fun <reified T> postAsync(url: String, headersMap: Map<String, String>, data: Any) = coroutineScope {
    return@coroutineScope httpClient.post(url) {
        for (element in headersMap) headers[element.key] = element.value
        setBody(data) // here?
    }.body<T>()
}

Please ensure that class is marked as ‘@Serializable’ and that the serialization compiler plugin is applied.

at app//kotlinx.serialization.internal.Platform_commonKt.serializerNotRegistered(Platform.common.kt:92)
at app//kotlinx.serialization.SerializersKt__SerializersKt.serializer(Serializers.kt:207)
at app//kotlinx.serialization.SerializersKt.serializer(Unknown Source)
at app//io.ktor.serialization.kotlinx.SerializerLookupKt.guessSerializer(SerializerLookup.kt:50)

With Ktor 2.0.0 this error is:

No request transformation found: {“search”:{“dates”:“current”}}

According to manual, now it’s needed to send a whole data class instead of its JSON. You need also to specify contentType.

val response: HttpResponse = client.post("http://localhost:8080/customer") {
    contentType(ContentType.Application.Json)
    setBody(Customer(3, "Jet", "Brains"))
}

There is a problem with settings appeared: encodeDefaults = false. Hope all my previous issues with Serialization fixed as for now and I can enable it.