I'm create a number of json messages for spray in scala using case classes. For example:
case class Foo(name: String, attrs: List[String])
implicit val fooFormat = jsonFormat2(Foo)
object Foo {
case class Invalid(error: String)
}
case class Bar(name: String, kv: Map[String, String])
implicit val barFormat = jsonFormat2(Bar)
In the above snippet, barFormat
compiles, but fooFormat
does not:
type mismatch; found : Foo.type required: (?, ?) => ?
Note: implicit value barFormat is not applicable here because it comes
after the application point and it lacks an explicit result type
I don't want to use barFormat
in place of fooFormat
, and I understand that a case class automatically generates a companion object, but I don't understand why there's a compiler error here, and the error message is difficult for me to decipher. Does anyone know what the problem is here and how to fix it, preferably without removing my Foo
companion object?
jsonFormat2
. – CalumniationjsonFormat2
is fairly cryptic:def jsonFormat2[P1, P2, T T)(implicit evidence$4: spray.json.DefaultJsonProtocol.JF[P1],implicit evidence$5: spray.json.DefaultJsonProtocol.JF[P2],implicit evidence$6: ClassManifest[T]): spray.json.RootJsonFormat[T]
– Bhilidef jsonFormat2[A :JF, B :JF, T <: Product :ClassManifest](construct: (A, B) => T): RootJsonFormat[T]
- you probably made a copy/paste mistake and removed theconstruct
. – Calumniationapply
directly? I was just copying an example of serializing case classes when I wrote the initial format, and it did not includeapply
, though it works fine until you "override" the case class's companion object. I guess this obscures the passage of theapply
method by simply providing the case class name. Is it bad practice to define a case class companion object? – Bhili