Question

i'm trying to do the following (i use spray-json_2.9.2 v1.2.3 because i use scala 2.9.2)

import spray.json._
import spray.json.DefaultJsonProtocol
import DefaultJsonProtocol._

case class TestMe(key: String, value: String)
object MyJsonProtocol extends DefaultJsonProtocol {
  implicit val prot = jsonFormat4(TestMe)
}

but i get compilation error:

TestMe.type (with underlying type object com...TestMe) [INFO] required: (?, ?, ?, ?) => ? [INFO] Note: implicit value prot is not applicable here because it comes after the application point and it lacks an explicit result type [INFO] implicit val prot = jsonFormat4(TestMe)

what am i doing wrong i just followed docs at: https://github.com/spray/spray-json#providing-jsonformats-for-case-classes

thanks

Was it helpful?

Solution

If you take a look at all jsonFormat signatures you'll that it requires a function, more precisely apply:

case class TestMe(key: String, value: String)
object MyJsonProtocol extends DefaultJsonProtocol {
  implicit val prot = jsonFormat2(TestMe.apply)
}

AND jsonFormat is not a macro function, it's just overloaded, so the number at the end should be equal to the number of args in case class

In this example your case class has two arguments, so you need jsonFormat2 and not jsonFormat4

And also better rename companion object MyJsonProtocol to TestMe, this will reduce the explicit import with implicits

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top