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:

Gson - 图1

Gradle (Groovy)

Gradle (Kotlin)

Maven

Gson - 图2

  1. implementation "io.ktor:ktor-gson:$ktor_version"

Register the Gson Converter

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

  1. import io.ktor.gson.*
  2. install(ContentNegotiation) {
  3. gson()
  4. }

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

  1. install(ContentNegotiation) {
  2. gson {
  3. setPrettyPrinting()
  4. disableHtmlEscaping()
  5. // ...
  6. }
  7. }

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