Scala 隐式UUID转换
我有一个用户模型Scala 隐式UUID转换,scala,spray-json,Scala,Spray Json,我有一个用户模型 case class User(name: String, email: String, password: Option[String] = None, key: Option[UUID] = None) 用喷雾器 object UserJsonSupport extends DefaultJsonProtocol with SprayJsonSupport { implicit val userFormat = jsonFormat4(User) } 它一直在工作,
case class User(name: String, email: String, password: Option[String] = None, key: Option[UUID] = None)
用喷雾器
object UserJsonSupport extends DefaultJsonProtocol with SprayJsonSupport {
implicit val userFormat = jsonFormat4(User)
}
它一直在工作,直到我将键字段从Option[String]
转换为Option[UUID]
,我现在得到两个编译错误:
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)
^
我的理解是,既然已经解决了,它就应该可以工作,而不需要提供我自己的UUID非序列化器。是我弄错了,还是完全是别的原因
有没有可能它不喜欢在
选项
中?它可能应该得到解决,但是,我最近遇到了同样的问题(在使用akka http v10.0.0
时),我能够通过定义以下内容来解决它
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")
}
}
}
这一解决方案是借鉴了美国的经验
更新: 我添加了一个库,其中包含了最常见的用例。