gpt4 book ai didi

scala - 喷json隐式UUID转换

转载 作者:行者123 更新时间:2023-12-04 15:10:06 25 4
gpt4 key购买 nike

我有一个用户模型

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

使用 Spray-json marshaller
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)
^

我的理解是,因为 this issue已解决,它应该可以正常工作而无需提供我自己的 UUID 反序列化程序。我错了还是完全是别的什么?

它可能不喜欢在 Option 里面吗? ?

最佳答案

它可能应该已经解决了,但是,我最近遇到了同样的问题(使用 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")
}
}
}

解决方案是从 Fidesmo API借来的.

更新:

我添加了一个经过最常见用例的库。
< Link here >

关于scala - 喷json隐式UUID转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31406825/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com