JSON support using Jackson

The Jackson feature allows you to handle JSON content in your application easily usingthe jackson library.

This feature is a ContentNegotiation converter.

This feature is defined in the class io.ktor.jackson.JacksonConverter in the artifact io.ktor:ktor-jackson:$ktor_version.

dependencies { implementation "io.ktor:ktor-jackson:$ktor_version"}

dependencies { implementation("io.ktor:ktor-jackson:$ktor_version")}

<project> … <dependencies> <dependency> <groupId>io.ktor</groupId> <artifactId>ktor-jackson</artifactId> <version>${ktor.version}</version> <scope>compile</scope> </dependency> </dependencies></project>

Basic usage

To install the feature by registering a JSON content convertor using Jackson:

  1. install(ContentNegotiation) {
  2. jackson {
  3. // Configure Jackson's ObjectMapper here
  4. }
  5. }

The jackson block is a convenient method for:

  1. register(ContentType.Application.Json, JacksonConverter(ObjectMapper().apply {
  2. // ...
  3. }.create()))

Configuration

Inside the jackson block, you have access to the ObjectMapperused to install the ContentNegotiation. To give you an idea of what is available:

  1. install(ContentNegotiation) {
  2. jackson {
  3. enable(SerializationFeature.INDENT_OUTPUT)
  4. enable(...)
  5. dateFormat = DateFormat.getDateInstance()
  6. disableDefaultTyping()
  7. convertValue(..., ...)
  8. ...
  9. }
  10. }