Ktor 1.6.7 Help


ContentNegotiation provides the built-in Gson converter for handing JSON data in your application.

Add dependencies

Before registering the Gson converter, you need to include the following artifacts in the build script:

implementation "io.ktor:ktor-gson:$ktor_version"
<dependency> <groupId>io.ktor</groupId> <artifactId>ktor-gson</artifactId> <version>${ktor_version}</version> </dependency>

Register the Gson converter

To register the Gson converter in your application, call the gson function:

import io.ktor.gson.* install(ContentNegotiation) { gson() }

Inside the gson block, you can access the GsonBuilder API, for example:

install(ContentNegotiation) { gson { setPrettyPrinting() disableHtmlEscaping() // ... } }

To learn how to receive and send data, see Receive and send data.

Last modified: 28 May 2021