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

x33g5p2x  于2022-03-02 转载在 其他  
字(1.0k)|赞(0)|评价(0)|浏览(535)
  1. fun getObjectMapper(): ObjectMapper {
  2. return ObjectMapper().registerModule(
  3. JavaTimeModule().addSerializer(
  4. LocalDateTime::class, LocalDateTimeSerializer(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
  5. ).addSerializer(
  6. ZonedDateTime::class, ZonedDateTimeSerializer(DateTimeFormatter.ISO_OFFSET_DATE_TIME)
  7. ).addSerializer(YearMonth::class, YearMonthSerializer(DateTimeFormatter.ofPattern("yyyy-MM")))
  8. ).configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true)
  9. }
  1. inline fun <reified T> ObjectMapper.objectToMap(obj: T): Map<String, Any> =
  2. this.readValue(this.writeValueAsString(obj), object : com.fasterxml.jackson.core.type.TypeReference<Map<String, Any>>() {})
  1. fun outputPdfFile(bill: Any): File {
  2. val jsonNode= getObjectMapper().objectToMap(bill)
  3. val customerName = BillingStackConfig.getConfig().fastoneBilling.customer.name
  4. return Path.of(
  5. PathConst.OUTPUT_PATH,
  6. PathConst.BILL_FOLDER,
  7. jsonNode["billingCycle"].toString(),
  8. "$customerName-${jsonNode.get("billingCycle")}-bill.pdf"
  9. ).toFile()
  10. }

相关文章