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

x33g5p2x  于2022-03-02 转载在 其他  
字(1.0k)|赞(0)|评价(0)|浏览(252)
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()
	}

相关文章

微信公众号

最新文章

更多