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.


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) {...}


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

dependencies {

    // Ktor client
//    implementation("io.ktor:ktor-client-serialization:$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).

fun sendPost() = runBlocking{
    val requestData = "{\"search\":{\"dates\":\"current\"}}"
    val json = Json.parseToJsonElement(requestData)
    val result = Ktor.postAsync<HttpResponse>("", 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?

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") {
    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.