jacksonktorjsr310

How to configure jackson-modules-java8 in Ktor


I am trying to configure jackson-modules-java8 with Ktor and Jackson but to no avail.

The module is added to gradle.build

dependencies {
    ...
    implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.12.0-rc2'
    ...
}

According to the Jackson docs I should do this:

ObjectMapper mapper = JsonMapper.builder()
    .addModule(new JavaTimeModule())
    .build();

But in Ktor I can only do this:

install(ContentNegotiation) {
    jackson {
        // `this` is the ObjectMapper
        this.enable(SerializationFeature.INDENT_OUTPUT)
        // what to do here?
    }
}

Solution

  • According with the official example if you want to add a module you could use

    registerModule

    as this:

    install(ContentNegotiation) {
        jackson {
            configure(SerializationFeature.INDENT_OUTPUT, true)
            setDefaultPrettyPrinter(DefaultPrettyPrinter().apply {
                indentArraysWith(DefaultPrettyPrinter.FixedSpaceIndenter.instance)
                indentObjectsWith(DefaultIndenter("  ", "\n"))
            })
            registerModule(JavaTimeModule())  // support java.time.* types
        }
    }