JSON serialization plugin for Ktor HTTP client (JVM platform)
npx @tessl/cli install tessl/maven-io-ktor--ktor-client-json-jvm@2.3.0The Ktor Client JSON plugin provides automatic JSON serialization and deserialization for HTTP client requests and responses. It supports multiple serialization backends including Gson, Jackson, and Kotlinx Serialization, with configurable content type matching and type filtering.
Note: This plugin is deprecated. Use the ContentNegotiation plugin for new projects.
implementation("io.ktor:ktor-client-json:2.3.13")import io.ktor.client.plugins.json.*
import io.ktor.client.plugins.gson.*
import io.ktor.client.plugins.jackson.*
import io.ktor.client.plugins.kotlinx.serializer.*import io.ktor.client.*
import io.ktor.client.plugins.json.*
import io.ktor.client.plugins.gson.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
// Install JSON plugin with default Gson serializer
val client = HttpClient {
install(JsonPlugin) {
serializer = GsonSerializer()
}
}
// Using data classes for automatic serialization
@Serializable
data class User(val name: String, val email: String)
// POST request with automatic JSON serialization
val response: HttpResponse = client.post("https://api.example.com/users") {
contentType(ContentType.Application.Json)
setBody(User("Alice", "alice@example.com"))
}
// GET request with automatic JSON deserialization
val user: User = client.get("https://api.example.com/users/1").body()The Ktor Client JSON plugin is built around several key components:
Core plugin configuration and installation for HTTP clients. Provides content type negotiation, serializer selection, and type filtering.
@Deprecated("Please use ContentNegotiation plugin")
class JsonPlugin internal constructor(
val serializer: JsonSerializer,
val acceptContentTypes: List<ContentType>,
private val receiveContentTypeMatchers: List<ContentTypeMatcher>,
private val ignoredTypes: Set<KClass<*>>
)
@KtorDsl
class Config {
var serializer: JsonSerializer?
var acceptContentTypes: List<ContentType>
var receiveContentTypeMatchers: List<ContentTypeMatcher>
fun accept(vararg contentTypes: ContentType)
fun receive(matcher: ContentTypeMatcher)
inline fun <reified T> ignoreType()
fun ignoreType(type: KClass<*>)
inline fun <reified T> removeIgnoredType()
fun removeIgnoredType(type: KClass<*>)
fun clearIgnoredTypes()
}
fun HttpClientConfig<*>.Json(block: JsonPlugin.Config.() -> Unit)Abstract serializer interface for implementing JSON backends. Defines the core serialization contract for request/response transformation.
@Deprecated("Please use ContentNegotiation plugin")
interface JsonSerializer {
fun write(data: Any, contentType: ContentType): OutgoingContent
fun write(data: Any): OutgoingContent
fun read(type: TypeInfo, body: Input): Any
}
expect fun defaultSerializer(): JsonSerializerGoogle Gson-based JSON serialization implementation with configurable Gson builder support.
@Deprecated("Please use ContentNegotiation plugin")
class GsonSerializer(block: GsonBuilder.() -> Unit = {}) : JsonSerializer {
override fun write(data: Any, contentType: ContentType): OutgoingContent
override fun read(type: TypeInfo, body: Input): Any
}Jackson-based JSON serialization implementation with configurable ObjectMapper support and advanced data binding features.
@Deprecated("Please use ContentNegotiation plugin")
class JacksonSerializer(
jackson: ObjectMapper = jacksonObjectMapper(),
block: ObjectMapper.() -> Unit = {}
) : JsonSerializer {
override fun write(data: Any, contentType: ContentType): OutgoingContent
override fun read(type: TypeInfo, body: Input): Any
}Kotlinx Serialization-based JSON implementation with configurable Json instance and advanced serialization features.
@Deprecated("Please use ContentNegotiation plugin")
class KotlinxSerializer(private val json: Json = DefaultJson) : JsonSerializer {
override fun write(data: Any, contentType: ContentType): OutgoingContent
override fun read(type: TypeInfo, body: Input): Any
companion object {
val DefaultJson: Json
}
}// Content type matching
interface ContentTypeMatcher {
fun contains(contentType: ContentType): Boolean
}
// Type information for deserialization
expect class TypeInfo {
val type: KClass<*>
val reifiedType: Type
val kotlinType: KType?
}
// HTTP content types
object ContentType {
object Application {
val Json: ContentType
}
}
// Outgoing HTTP content
abstract class OutgoingContent
class TextContent(
val text: String,
val contentType: ContentType
) : OutgoingContent
// Input stream for reading response bodies
interface Input {
fun readText(): String
}