I have a case class containing varargs, with an implicit jsonFormat as follows:
import spray.json._
case class Colors(name: String*)
object MyJsonProtocol extends DefaultJsonProtocol {
implicit val colorFormat = jsonFormat1(Colors)
}
import MyJsonProtocol._
Colors("CadetBlue").toJson
It raises an error:
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)
^
I have also tried:
implicit val colorFormat = jsonFormat1(Colors.apply)
which caused a different (runtime!) exception:
java.lang.RuntimeException: Cannot automatically determine case class field names and order for 'Colors', please use the 'jsonFormat' overload with explicit field name specification
The following:
implicit val colorFormat = jsonFormat(Colors, "name")
raises the former error
It is even possible to define implicit jsonFormat for case class with varargs?
List[String]
instead of varargs, or make your own unmarshaller. – Droit