Json

    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.

    1. dependencies {
    2. implementation "io.ktor:ktor-client-json:$ktor_version"
    3. }
    1. dependencies {
    2. implementation("io.ktor:ktor-client-json:$ktor_version")
    3. }
    1. <project>
    2. ...
    3. <dependencies>
    4. <dependency>
    5. <groupId>io.ktor</groupId>
    6. <artifactId>ktor-client-json</artifactId>
    7. <version>${ktor.version}</version>
    8. <scope>compile</scope>
    9. </dependency>
    10. </dependencies>
    11. </project>
    1. dependencies {
    2. implementation "io.ktor:ktor-client-json-jvm:$ktor_version"
    3. }
    1. dependencies {
    2. implementation("io.ktor:ktor-client-json-jvm:$ktor_version")
    3. }
    1. <project>
    2. ...
    3. <dependencies>
    4. <dependency>
    5. <groupId>io.ktor</groupId>
    6. <artifactId>ktor-client-json-jvm</artifactId>
    7. <version>${ktor.version}</version>
    8. <scope>compile</scope>
    9. </dependency>
    10. </dependencies>
    1. dependencies {
    2. implementation "io.ktor:ktor-client-json-native:$ktor_version"
    3. }
    1. dependencies {
    2. implementation("io.ktor:ktor-client-json-native:$ktor_version")
    3. }
    1. implementation "io.ktor:ktor-client-json-js:$ktor_version"
    2. }
    1. dependencies {
    2. implementation("io.ktor:ktor-client-json-js:$ktor_version")
    3. }
    1. <project>
    2. ...
    3. <dependencies>
    4. <dependency>
    5. <groupId>io.ktor</groupId>
    6. <artifactId>ktor-client-json-js</artifactId>
    7. <version>${ktor.version}</version>
    8. <scope>compile</scope>
    9. </dependency>
    10. </dependencies>
    11. </project>

    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 for Native as well as for JavaScript.

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

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

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

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

    «««< HEADTo 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.

    1. dependencies {
    2. implementation "io.ktor:ktor-client-serialization:$ktor_version"
    3. }
    1. implementation("io.ktor:ktor-client-serialization:$ktor_version")
    2. }
    1. dependencies {
    2. implementation "io.ktor:ktor-client-serialization-jvm:$ktor_version"
    3. }
    1. dependencies {
    2. implementation("io.ktor:ktor-client-serialization-jvm:$ktor_version")
    3. }
    1. <project>
    2. ...
    3. <dependencies>
    4. <dependency>
    5. <groupId>io.ktor</groupId>
    6. <artifactId>ktor-client-serialization-jvm</artifactId>
    7. <version>${ktor.version}</version>
    8. <scope>compile</scope>
    9. </dependency>
    10. </dependencies>
    11. </project>
    1. dependencies {
    2. implementation "io.ktor:ktor-client-serialization-native:$ktor_version"
    3. }
    1. dependencies {
    2. implementation("io.ktor:ktor-client-serialization-native:$ktor_version")
    3. }
    1. <project>
    2. ...
    3. <dependencies>
    4. <dependency>
    5. <groupId>io.ktor</groupId>
    6. <artifactId>ktor-client-serialization-native</artifactId>
    7. <version>${ktor.version}</version>
    8. <scope>compile</scope>
    9. </dependency>
    10. </dependencies>
    11. </project>
    1. dependencies {
    2. implementation "io.ktor:ktor-client-serialization-js:$ktor_version"
    3. }
    1. dependencies {
    2. implementation("io.ktor:ktor-client-serialization-js:$ktor_version")