Json

note

Json - 图1

This help topic is in development and will be updated in the future.

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

  1. val client = HttpClient(HttpClientEngine) {
  2. install(JsonFeature)
  3. }

You have a full example using JSON.

note

Json - 图2

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

  1. val client = HttpClient(HttpClientEngine) {
  2. install(JsonFeature) {
  3. serializer = GsonSerializer()
  4. }
  5. }

note

Json - 图3

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

Jackson

  1. val client = HttpClient(HttpClientEngine) {
  2. install(JsonFeature) {
  3. serializer = JacksonSerializer()
  4. }
  5. }

note

Json - 图4

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

Kotlinx.Serialization

  1. val client = HttpClient(HttpClientEngine) {
  2. install(JsonFeature) {
  3. serializer = KotlinxSerializer()
  4. }
  5. }

note

Json - 图5

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.