Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spray json implicit UUID conversion

I have a User model

case class User(name: String, email: String, password: Option[String] = None, key: Option[UUID] = None)

With a spray-json marshaller

object UserJsonSupport extends DefaultJsonProtocol with SprayJsonSupport {
  implicit val userFormat = jsonFormat4(User)
}

It was working until I converted the key field from Option[String] to Option[UUID] and I now get two compilation errors:

Error:(8, 40) could not find implicit value for evidence parameter of type in.putfood.http.UserJsonSupport.JF[Option[java.util.UUID]]
  implicit val userFormat = jsonFormat4(User)
                                       ^
Error:(8, 40) not enough arguments for method jsonFormat4: (implicit evidence$16: in.putfood.http.UserJsonSupport.JF[String], implicit evidence$17: in.putfood.http.UserJsonSupport.JF[String], implicit evidence$18: in.putfood.http.UserJsonSupport.JF[Option[String]], implicit evidence$19: in.putfood.http.UserJsonSupport.JF[Option[java.util.UUID]], implicit evidence$20: ClassManifest[in.putfood.model.User])spray.json.RootJsonFormat[in.putfood.model.User].
Unspecified value parameters evidence$19, evidence$20.
  implicit val userFormat = jsonFormat4(User)
                                   ^

My understanding was that since this issue was resolved, it should just work without needed to provide my own UUID unserializer. Am I mistaken or is it something else entirely?

Is it possible it doesn't like being inside an Option?

like image 533
GoldenFish Avatar asked Jul 14 '15 12:07

GoldenFish


1 Answers

It probably should've been resolved, however, I ran into the same issue lately (While using akka-http v10.0.0) and I was able to resolve it by defining the following

  implicit object UUIDFormat extends JsonFormat[UUID] {
    def write(uuid: UUID) = JsString(uuid.toString)
    def read(value: JsValue) = {
      value match {
        case JsString(uuid) => UUID.fromString(uuid)
        case _              => throw new DeserializationException("Expected hexadecimal UUID string")
      }
    }
  }

The solution was borrowed from Fidesmo API.


Update:

I added a library that go through the most common use cases. <Link here>

like image 70
mamdouh alramadan Avatar answered Nov 20 '22 10:11

mamdouh alramadan