2

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?
    }
}
Thomas
  • 793
  • 1
  • 8
  • 16

1 Answers1

2

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
    }
}
shadowsheep
  • 14,048
  • 3
  • 67
  • 77
  • That works great. I also had to disable a feature to get the result I wanted, but now it works: `disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)` – Thomas Nov 19 '20 at 09:50