Reputation: 207
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
?
Upvotes: 7
Views: 1951
Reputation: 9715
The issue mentioned referenced a PR that was closed, not merged. The reason is also provided there:
I want to cancel this Pull Request – there are instances where non-String serialisation is useful.
In scalad, we're creating custom marshallers for Date and UUID so that the Mongo backend can serialise them correctly.
So, please stick with mamdouh's answer. It's not a big deal to create a custom format.
Upvotes: 0
Reputation: 8528
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>
Upvotes: 6