Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?
    }
}
like image 430
Thomas Avatar asked Oct 30 '25 05:10

Thomas


1 Answers

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
    }
}
like image 117
shadowsheep Avatar answered Nov 01 '25 02:11

shadowsheep