根据这里描述的内容,我应该使用 JSR-310 表示而不是数字表示来序列化ZonedDateTime
对象。但是,我得到了数字表示。可能是什么问题?
这是我配置正在使用的映射器的方式:
object JsonFormatter {
private val mapper = new ObjectMapper() with ScalaObjectMapper
mapper
.registerModule(DefaultScalaModule)
.registerModule(new JavaTimeModule())
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
def fromJson[T](json: String)(implicit m: Manifest[T]): T = mapper.readValue[T](json.getBytes)
def toJson(value: Any): String = mapper.writeValueAsString(value)
}
这是我得到的反序列化示例
{"from":"CityA","to":"CityB","departureAt":1554970560.000000000,"arriveAt":1554984660.000000000,"duration":{"length":65,"unit":"MINUTES","finite":true}}
对于以下事例类:
case class Stock(from: String,
to: String,
departureAt: ZonedDateTime,
arriveAt: ZonedDateTime,
duration: Duration)
显然WRITE_DATES_AS_TIMESTAMPS
默认情况下启用该功能,因此我不得不通过添加以下内容来禁用它:
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)