Deprecated kotlinx.serialization support for Ktor HTTP client JSON plugin.
npx @tessl/cli install tessl/maven-io-ktor--ktor-client-serialization-jvm@3.2.0Ktor Client Serialization provides deprecated kotlinx.serialization support for Ktor HTTP client JSON plugin. This package offers the KotlinxSerializer class that enables automatic serialization and deserialization of Kotlin classes marked with @Serializable annotation when making HTTP requests and processing responses.
⚠️ Deprecation Notice: This package has been deprecated with ERROR level and replaced by the ContentNegotiation plugin which offers better performance and broader format support. New applications should use ContentNegotiation instead.
implementation("io.ktor:ktor-client-serialization-jvm:3.2.0")import io.ktor.client.plugins.kotlinx.serializer.KotlinxSerializer
import io.ktor.client.plugins.json.*
import kotlinx.serialization.json.Jsonimport io.ktor.client.plugins.kotlinx.serializer.KotlinxSerializer
import io.ktor.client.plugins.json.*
import kotlinx.serialization.json.Json
// Create serializer with default configuration
val serializer = KotlinxSerializer()
// Create serializer with custom JSON configuration
val customJson = Json {
isLenient = true
ignoreUnknownKeys = true
}
val customSerializer = KotlinxSerializer(customJson)
// Use with Ktor HTTP client (deprecated pattern)
@Suppress("DEPRECATION_ERROR")
val client = HttpClient {
install(JsonFeature) {
serializer = KotlinxSerializer()
}
}Main serializer class implementing JsonSerializer interface for kotlinx.serialization integration.
@Deprecated(
"Please use ContentNegotiation plugin and its converters",
level = DeprecationLevel.ERROR
)
class KotlinxSerializer(
private val json: Json = DefaultJson
) : JsonSerializer {
/**
* Serialize data to JSON content with specified content type
*/
override fun write(data: Any, contentType: ContentType): OutgoingContent
/**
* Serialize data to JSON content with default JSON content type
*/
fun write(data: Any): OutgoingContent
/**
* Deserialize JSON content to specified type using TypeInfo
*/
override fun read(type: TypeInfo, body: Input): Any
companion object {
/**
* Default Json configuration for KotlinxSerializer
*/
val DefaultJson: Json
}
}Serializes Kotlin objects to JSON content for HTTP requests.
/**
* Serialize data to JSON content with specified content type
* @param data - Any object to serialize (must be @Serializable)
* @param contentType - HTTP content type for the output
* @return OutgoingContent containing serialized JSON
*/
override fun write(data: Any, contentType: ContentType): OutgoingContent
/**
* Serialize data to JSON content with default JSON content type (Application/Json)
* @param data - Any object to serialize (must be @Serializable)
* @return OutgoingContent containing serialized JSON
*/
fun write(data: Any): OutgoingContentDeserializes JSON content from HTTP responses to Kotlin objects.
/**
* Deserialize JSON content to specified type using TypeInfo
* @param type - TypeInfo specifying the target type
* @param body - Input stream containing JSON data
* @return Deserialized object of the specified type
*/
override fun read(type: TypeInfo, body: Input): AnyDefault Json configuration used when no custom configuration is provided.
companion object {
/**
* Default Json configuration for KotlinxSerializer with conservative settings:
* - isLenient = false
* - ignoreUnknownKeys = false
* - allowSpecialFloatingPointValues = true
* - useArrayPolymorphism = false
*/
val DefaultJson: Json
}// From kotlinx.serialization
interface Json {
val serializersModule: SerializersModule
fun <T> encodeToString(serializer: SerializationStrategy<T>, value: T): String
fun <T> decodeFromString(deserializer: DeserializationStrategy<T>, string: String): T
}
// From Ktor
interface JsonSerializer {
fun write(data: Any, contentType: ContentType): OutgoingContent
fun write(data: Any): OutgoingContent // Default implementation delegates to write(data, ContentType.Application.Json)
fun read(type: TypeInfo, body: Input): Any
}
interface ContentType
interface OutgoingContent
interface TypeInfo
interface InputThe serializer automatically handles the following types through kotlinx.serialization:
This package supports multiple Kotlin platforms:
Platform-specific initializers automatically register the serializer with the appropriate plugin system on each platform.
Instead of this deprecated package, use:
// New approach with ContentNegotiation
import io.ktor.client.*
import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.serialization.kotlinx.json.*
val client = HttpClient {
install(ContentNegotiation) {
json()
}
}The ContentNegotiation plugin provides: