Gson
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:
Gradle (Groovy)
Gradle (Kotlin)
Maven
implementation "io.ktor:ktor-gson:$ktor_version"
Register the Gson Converter
To register the Gson converter in your application, call the gson method:
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.