Accepted answer

From your compile error, it looks like jsonFormat2 expects a two-argument function. Do you mean to pass the constructors of Foo and Bar into it? If so, you should do Foo.apply and Bar.apply.


Spray.json documentation suggests an easier way than @wingedsubmariner's suggestion:

If you explicitly declare the companion object for your case class the notation above will stop working. You'll have to explicitly refer to the companion object's apply method to fix this:

So the correction for the question becomes:

implicit val fooFormat = jsonFormat2(Foo.apply)

Added: This is in fact what also @geoffliu suggests in his answer.


Case class companion objects will by default extend one of the function traits. object Foo would have extended ((String, List[String]) => Foo) but when you manually defined it you didn't extend that trait. This is why you couldn't pass it to jsonFormat2, which was expecting a (?, ?) => ?. If you make the following change your code should compile:

object Foo extends ((String, List[String]) => Foo) {

Related Query

More Query from same tag