0
点赞
收藏
分享

微信扫一扫

koltin 如何使用Jackson的ObjectMapper()获取json中的值

夕阳孤草 2022-10-26 阅读 217


fun getObjectMapper(): ObjectMapper {
return ObjectMapper().registerModule(
JavaTimeModule().addSerializer(
LocalDateTime::class, LocalDateTimeSerializer(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
).addSerializer(
ZonedDateTime::class, ZonedDateTimeSerializer(DateTimeFormatter.ISO_OFFSET_DATE_TIME)
).addSerializer(YearMonth::class, YearMonthSerializer(DateTimeFormatter.ofPattern("yyyy-MM")))
).configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true)
}

inline fun <reified T> ObjectMapper.objectToMap(obj: T): Map<String, Any> =
this.readValue(this.writeValueAsString(obj), object : com.fasterxml.jackson.core.type.TypeReference<Map<String, Any>>() {})

fun outputPdfFile(bill: Any): File {
val jsonNode= getObjectMapper().objectToMap(bill)
val customerName = BillingStackConfig.getConfig().fastoneBilling.customer.name
return Path.of(
PathConst.OUTPUT_PATH,
PathConst.BILL_FOLDER,
jsonNode["billingCycle"].toString(),
"$customerName-${jsonNode.get("billingCycle")}-bill.pdf"
).toFile()
}


举报

相关推荐

0 条评论