Json

预计阅读时间: 3 分钟

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.

本特性在构件 io.ktor:ktor-client-json:$ktor_version 中的 io.ktor.client.features.json.JsonFeature 类中定义
dependencies { implementation "io.ktor:ktor-client-json:$ktor_version" }
dependencies { implementation("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>
dependencies { implementation "io.ktor:ktor-client-json-jvm:$ktor_version" }
dependencies { implementation("io.ktor:ktor-client-json-jvm:$ktor_version") }
<project> ... <dependencies> <dependency> <groupId>io.ktor</groupId> <artifactId>ktor-client-json-jvm</artifactId> <version>${ktor.version}</version> <scope>compile</scope> </dependency> </dependencies> </project>
dependencies { implementation "io.ktor:ktor-client-json-native:$ktor_version" }
dependencies { implementation("io.ktor:ktor-client-json-native:$ktor_version") }
<project> ... <dependencies> <dependency> <groupId>io.ktor</groupId> <artifactId>ktor-client-json-native</artifactId> <version>${ktor.version}</version> <scope>compile</scope> </dependency> </dependencies> </project>
dependencies { implementation "io.ktor:ktor-client-json-js:$ktor_version" }
dependencies { implementation("io.ktor:ktor-client-json-js:$ktor_version") }
<project> ... <dependencies> <dependency> <groupId>io.ktor</groupId> <artifactId>ktor-client-json-js</artifactId> <version>${ktor.version}</version> <scope>compile</scope> </dependency> </dependencies> </project>

To use this feature with Kotlin/JS, you need to include the io.ktor:ktor-client-json-js artifact.

Serializers

The JsonFeature has a default serializer(implicitly obtained or by calling defaultSerializer()) based on a ServiceLoader on JVM(supporting Gson or Jackson depending on the artifact included), and a serializer based on kotlinx.serialization for Native as well as for JavaScript.

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-serialization-jvm artifact on the JVM and io.ktor:ktor-client-serialization-native on iOS.

dependencies { implementation "io.ktor:ktor-client-serialization:$ktor_version" }
dependencies { implementation("io.ktor:ktor-client-serialization:$ktor_version") }
<project> ... <dependencies> <dependency> <groupId>io.ktor</groupId> <artifactId>ktor-client-serialization</artifactId> <version>${ktor.version}</version> <scope>compile</scope> </dependency> </dependencies> </project>
dependencies { implementation "io.ktor:ktor-client-serialization-jvm:$ktor_version" }
dependencies { implementation("io.ktor:ktor-client-serialization-jvm:$ktor_version") }
<project> ... <dependencies> <dependency> <groupId>io.ktor</groupId> <artifactId>ktor-client-serialization-jvm</artifactId> <version>${ktor.version}</version> <scope>compile</scope> </dependency> </dependencies> </project>
dependencies { implementation "io.ktor:ktor-client-serialization-native:$ktor_version" }
dependencies { implementation("io.ktor:ktor-client-serialization-native:$ktor_version") }
<project> ... <dependencies> <dependency> <groupId>io.ktor</groupId> <artifactId>ktor-client-serialization-native</artifactId> <version>${ktor.version}</version> <scope>compile</scope> </dependency> </dependencies> </project>
dependencies { implementation "io.ktor:ktor-client-serialization-js:$ktor_version" }
dependencies { implementation("io.ktor:ktor-client-serialization-js:$ktor_version") }
<project> ... <dependencies> <dependency> <groupId>io.ktor</groupId> <artifactId>ktor-client-serialization-js</artifactId> <version>${ktor.version}</version> <scope>compile</scope> </dependency> </dependencies> </project>