JSON support using Jackson

预计阅读时间:1分钟

Jackson功能使您可以使用jackson库在应用程序中轻松处理JSON内容.

此功能是ContentNegotiation转换器.

此功能定义在类io.ktor.jackson.JacksonConverter在神器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

要通过使用Jackson注册JSON内容转换器来安装功能,请执行以下操作:

install(ContentNegotiation) {
    jackson {
        // Configure Jackson's ObjectMapper here
    }
}

jackson积木是一种方便的方法:

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

Configuration

jackson块内,您​​可以访问用于安装ContentNegotiation的ObjectMapper . 让您了解可用的内容:

install(ContentNegotiation) {
    jackson {
        enable(SerializationFeature.INDENT_OUTPUT)
        enable(...)
        dateFormat = DateFormat.getDateInstance()
        disableDefaultTyping()
        convertValue(..., ...)
        ...
    }
}

by  ICOPY.SITE