JodaTime 序列化问题

JodaTime Serialization issue

我有一个现有的应用程序正在使用 Java JodaTime。但是,在升级到最新的 json4s-core 库 3.6.0-M3 之后,将带有时区的日期字符串转换为日期时出现以下错误。

Caused by: java.lang.IllegalArgumentException: No instant converter found for type: org.json4s.ext.DateParser$ZonedInstant

当我编写一个自定义 DateTime 序列化器时会发生这种情况,该序列化程序具有多种格式的回退:

 case JString(s) ⇒ Try(dateTimeFormat.parseDateTime(s)).getOrElse(new DateTime(DateParser.parse(s, format)))

导致问题的示例字符串:2018-05-02T21:43:29Z

我确定我使用的是 jodatime 2.9.2 和匹配的 json4s-ext 库

我会留下我的答案以防有人遇到类似问题。我意识到,由于我正在覆盖默认的 DateTime 序列化程序,因此我需要对我的自定义序列化程序进行以下更改以处理 ZonedInstant

case JString(s) ⇒ Try(dateTimeFormat.parseDateTime(s)).getOrElse({
    val zonedInstant = DateParser.parse(s, format)
    new DateTime(zonedInstant.instant, DateTimeZone.forTimeZone(zonedInstant.timezone))
  })