是否有开箱即用的方法来使用 json4s 解析 2 元组列表?

Is there an out of the box way to parse a list of 2-tuples with json4s?

鉴于:

implicit val formats = DefaultFormats

val json =
  """[
      {"myType":"type1","things":["1","2","3","4","5","6"]},
      {"myType":"type1","things":["1","2","3","4","5","6"]}
    ]"""

case class Stuff(myType: String, things: List[String])
parse(json).extract[List[Stuff]]

Scala 在运行时产生此错误:

An exception or error caused a run to abort: Temp and Temp$$anonfun$Stuff disagree on InnerClasses attribute
java.lang.IncompatibleClassChangeError: Temp and Temp$$anonfun$Stuff disagree on InnerClasses attribute

是否有开箱即用的方法来使用 json4s 解析它,或者这是自定义序列化程序的情况?

json4s 版本为 3.5.2

由于系统限制,scala 版本较旧,2.10.4

范围内是否有隐式 Formats 对象?

extract 方法需要隐式格式参数。 Json4s 为列表等内容提供默认格式。

下面的代码适合我

import org.json4s.DefaultFormats
import org.json4s.native.JsonMethods.parse

implicit val formats = DefaultFormats

val json =
  """[
      {"myType":"type1","things":["1","2","3","4","5","6"]},
      {"myType":"type1","things":["1","2","3","4","5","6"]}
  ]"""

case class Stuff(myType: String, things: List[String])
parse(json).extract[List[Stuff]]


res0: List[Stuff] = List(Stuff(type1,List(1, 2, 3, 4, 5, 6)), Stuff(type1,List(1, 2, 3, 4, 5, 6)))

json4s 版本 3.2.11

scala 版本 2.11

如果你在范围内有一个隐式格式化程序,它可能是 json4s Reflector 中的一个错误。在不同的地方声明您的 Stuff 案例 class 可能会解决问题。有关详细信息,请参阅此 github 问题 https://github.com/json4s/json4s/issues/84