JsonFeature

Estimated reading time: 1 minute

Processes the request and the response payload as JSON, serializing and de-serializing them using a specific serializer: JsonSerializer.

val client = HttpClient(HttpClientEngine) {
    install(JsonFeature)
}

You have a full example using JSON.

This feature is defined in the class io.ktor.client.features.json.JsonFeature in the artifact io.ktor:ktor-client-json:$ktor_version.
dependencies { compile "io.ktor:ktor-client-json:$ktor_version" }
<project> ... <dependencies> <dependency> <groupId>io.ktor</groupId> <artifactId>ktor-client-json</artifactId> <version>${ktor.version}</version> <scope>compile</scope> </dependency> </dependencies> </project>

Serializers

The JsonFeature has a default serializer based on a ServiceLoader on JVM, and a serializer based on kotlinx.serialization for Native.

You can also get the default serializer by calling io.ktor.client.features.json.defaultSerializer()

Gson

val client = HttpClient(HttpClientEngine) {
    install(JsonFeature) {
        serializer = GsonSerializer()
    }
}

To use this feature, you need to include io.ktor:ktor-client-gson artifact.

Jackson

val client = HttpClient(HttpClientEngine) {
    install(JsonFeature) {
        serializer = JacksonSerializer()
    }
}

To use this feature, you need to include io.ktor:ktor-client-jackson artifact.

Kotlinx.Serialization

val client = HttpClient(HttpClientEngine) {
    install(JsonFeature) {
        serializer = KotlinxSerializer()
    }
}

To use this feature, you need to include io.ktor:ktor-client-jvm artifact on the jvm.