Json

预计阅读时间:2分钟

将请求和响应有效负载处理为JSON,并使用特定的serializer: JsonSerializer对其进行序列化和反序列serializer: JsonSerializer .

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

您有使用JSON完整示例 .

此功能定义在类io.ktor.client.features.json.JsonFeature在神器io.ktor:ktor-client-json:$ktor_version .
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>

要将此功能与Kotlin / JS一起使用,您需要包括io.ktor:ktor-client-json-js工件.

Serializers

JsonFeature具有一个默认的序列化器(默认获得)或基于JVM上的ServiceLoader隐式获取(通过调用defaultSerializer() )(根据所包含的工件支持Gson或Jackson),以及一个基于kotlinx.serialization的序列化器(对于Native以及JavaScript).

您还可以通过调用io.ktor.client.features.json.defaultSerializer()获得默认的序列化器.

Gson

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

要使用此功能,您需要包括io.ktor:ktor-client-gson工件.

Jackson

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

要使用此功能,您需要包括io.ktor:ktor-client-jackson工件.

Kotlinx.Serialization

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

要使用此功能,您需要在JVM上包括io.ktor:ktor-client-serialization-jvm工件,在iOS上包括io.ktor:ktor-client-serialization-native .

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>

by  ICOPY.SITE