Json4s:分解为JValue会产生ArrayIndexOutOfBoundsException

时间:2017-03-30 16:04:35

标签: scala json4s

我正在使用Extraction.decompose生成JValue并且它间歇性地失败。我要求它分解以下案例类的Seq

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$4.format(Formats.scala:358)
    at org.json4s.ext.DateTimeSerializer$$anonfun$$lessinit$greater$4$$anonfun$apply$8.applyOrElse(JodaTimeSerializers.scala:78)
    at scala.PartialFunction$OrElse.apply(PartialFunction.scala:167)
    at org.json4s.ext.InstantSerializer$$anonfun$$lessinit$greater$3$$anonfun$apply$6.applyOrElse(JodaTimeSerializers.scala:57)
    at scala.PartialFunction$OrElse.apply(PartialFunction.scala:167)
    at org.json4s.ext.DurationSerializer$$anonfun$$lessinit$greater$2$$anonfun$apply$4.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$1(Extraction.scala:110)
    at org.json4s.Extraction$.decomposeObject$1(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

我已经使用ArrayIndexOutOfBoundsExceptionsun.util.calendar.BaseCalendar.getCalendarDateFromFixedDate搜索了SimpleDateFormat个问题,并且有很多关于SimpleDateFormat不是线程安全的帖子。这似乎可以解释错误但我可以看到Json4s wraps the dateFormatter in a ThreadLocal,我理解它应该使其线程安全。

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

我使用的是Scala 2.11.8,Json4s 3.5.1,OpenJDK 1.8.0。

干杯, 保罗

1 个答案:

答案 0 :(得分:0)

您正在使ThreadLocal dateFormatter val而不是defDefaultFormats的链接来源相同,从而击败def,因此所有主题都会结束指的是同一个例子。当然,当你将其设为setTimeZone时,需要在定义中完成width