我有一个包含可变参数的案例类,其隐式 jsonFormat 如下:
import spray.json._
case class Colors(name: String*)
object MyJsonProtocol extends DefaultJsonProtocol {
implicit val colorFormat = jsonFormat1(Colors)
}
import MyJsonProtocol._
Colors("CadetBlue").toJson
它引发了一个错误:
error: type mismatch;
found : Color2.type
required: Seq[String] => Color2
Note: implicit value colorFormat is not applicable here because it comes after the application point and it lacks an explicit result type
implicit val colorFormat = jsonFormat1(Color2)
^
我也试过:
implicit val colorFormat = jsonFormat1(Colors.apply)
这导致了不同的(运行时!)异常:
java.lang.RuntimeException: Cannot automatically determine case class field names and order for 'Colors', please use the 'jsonFormat' overload with explicit field name specification
以下:
implicit val colorFormat = jsonFormat(Colors, "name")
引发前一个错误
甚至可以为带有可变参数的案例类定义隐式 jsonFormat?