Json4s:分解为 JValue 给出 ArrayIndexOutOfBoundsException

Json4s: Decomposing to JValue gives ArrayIndexOutOfBoundsException

我正在使用 Extraction.decompose 生成 JValue,但它会间歇性地失败。我要求它分解以下情况的 Seq class:

case class Item(locators: Seq[String], dateAdded: DateTime = new DateTime(0L), newVersionSinceCuration: Option[Boolean] = None)

DateTimeorg.joda.time.DateTime

同一行代码在大多数情况下都能正常工作,但每隔几分钟就会失败,并显示以下堆栈跟踪:

java.lang.ArrayIndexOutOfBoundsException: 14
    at sun.util.calendar.BaseCalendar.getCalendarDateFromFixedDate(BaseCalendar.java:453)
    at java.util.GregorianCalendar.computeFields(GregorianCalendar.java:2397)
    at java.util.GregorianCalendar.computeFields(GregorianCalendar.java:2312)
    at java.util.Calendar.complete(Calendar.java:2268)
    at java.util.Calendar.get(Calendar.java:1826)
    at java.text.SimpleDateFormat.subFormat(SimpleDateFormat.java:1119)
    at java.text.SimpleDateFormat.format(SimpleDateFormat.java:966)
    at java.text.SimpleDateFormat.format(SimpleDateFormat.java:936)
    at java.text.DateFormat.format(DateFormat.java:345)
    at org.json4s.DefaultFormats$$anon.format(Formats.scala:358)
    at org.json4s.ext.DateTimeSerializer$$anonfun$$lessinit$greater$$anonfun$apply.applyOrElse(JodaTimeSerializers.scala:78)
    at scala.PartialFunction$OrElse.apply(PartialFunction.scala:167)
    at org.json4s.ext.InstantSerializer$$anonfun$$lessinit$greater$$anonfun$apply.applyOrElse(JodaTimeSerializers.scala:57)
    at scala.PartialFunction$OrElse.apply(PartialFunction.scala:167)
    at org.json4s.ext.DurationSerializer$$anonfun$$lessinit$greater$$anonfun$apply.applyOrElse(JodaTimeSerializers.scala:47)
    at scala.PartialFunction$OrElse.apply(PartialFunction.scala:167)
    at scala.PartialFunction$class.applyOrElse(PartialFunction.scala:123)
    at scala.collection.AbstractMap.applyOrElse(Map.scala:59)
    at scala.PartialFunction$OrElse.apply(PartialFunction.scala:167)
    at org.json4s.Extraction$.internalDecomposeWithBuilder(Extraction.scala:146)
    at org.json4s.Extraction$.addField(Extraction.scala:110)
    at org.json4s.Extraction$.decomposeObject(Extraction.scala:140)
    at org.json4s.Extraction$.internalDecomposeWithBuilder(Extraction.scala:228)
    at org.json4s.Extraction$.internalDecomposeWithBuilder(Extraction.scala:189)
    at org.json4s.Extraction$.decomposeWithBuilder(Extraction.scala:64)
    at org.json4s.Extraction$.decompose(Extraction.scala:242)

    ...

所以它似乎无法解析DateTime。我根据 Json4s 文档使用以下内容覆盖日期格式:

implicit def json4sFormats = new DefaultFormats {
    override val dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX")
    dateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"))
} ++ JodaTimeSerializers.all

我用谷歌搜索了 ArrayIndexOutOfBoundsException sun.util.calendar.BaseCalendar.getCalendarDateFromFixedDateSimpleDateFormat 的问题,并且有很多关于 SimpleDateFormat 不是线程安全的帖子。这似乎可以解释错误,但我可以看到 Json4s wraps the dateFormatter in a ThreadLocal,据我所知应该使它成为线程安全的。

对这里可能发生的事情有什么想法吗?

我正在使用 Scala 2.11.8、Json4s 3.5.1、OpenJDK 1.8.0。

干杯, 保罗

您正在通过将 dateFormatter 设为 val 而不是 DefaultFormats 的链接源中的 def 来击败 ThreadLocal,因此所有线程最终指的是同一个实例。当然,当你做成def的时候,需要在定义里面做setTimeZone