Json marshalling fails for case classes with a companion object


(Ali A) #1

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?


(PJ Fanning) #2

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


(Ali A) #3

Thank you!