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.