Json marshalling fails for case classes with a companion object

If I have:

case class Foo(a: String, b: Int)

and:

implicit val foo: RootJsonFormat[Foo] = jsonFormat2(Foo)

that works. However if I add:

case object Foo {
  def apply(bar:Bar):Foo = Foo(bar.a, bar.b)
}

Then the compile on the marshalling line above fails:

Error:(18, 65) type mismatch;
 found   : io.xx.Foo.type
 required: (?, ?) => io.xx.Foo
 implicit val foo: RootJsonFormat[Foo] = jsonFormat2(Foo)

Any ideas how to fix this or what I’m doing wrong?

This link might be of help: https://github.com/spray/spray-json/issues/74

1 Like

Thank you!