文章目录 @Configuration class JacksonConfiguration { /** * Support for Java date and time API. * @return the corresponding Jackson module. */ @Bean fun javaTimeModule() = JavaTimeModule() @Bean fun jdk8TimeModule() = Jdk8Module() } object JsonUtils { val objectMapper: ObjectMapper = jacksonObjectMapper().registerModule(JavaTimeModule()).registerModule(Jdk8Module()) fun <T> toJson(data: T): String { return if (data is String) { data } else objectMapper.writeValueAsString(data) } inline fun <reified T> fromJson(content: String): T { return objectMapper.readValue(content) } } fun <T> T.toJson(): String { return JsonUtils.toJson(this) }