【发布时间】:2012-11-01 21:48:38
【问题描述】:
我正在尝试编写一个通用提取器,用于使用 spray 和 spray-json 解析 json POST 正文。
但是,我很难让它与多个模型一起使用。这是服务对象中的 case 语句:
import MyJsonProtocol._
...
def receive = {
case Post (Routes.person.post, p: Person) => sender ! Ok(Actions.person.post(p))
case Get (Routes.foo.forId(x)) => sender ! Ok(x)
case _ => sender ! Ok("No handler")
}
这是我写的提取器(只要在 case 语句的范围内只有一个模型的 JsonReader 就可以工作):
//NB. Json.parse returns an Option[T]
object Post extends Request {
def unapply[T:JsonReader](req: HttpRequest): Option[(String, T)] = req match {
case HttpRequest(POST, url, _, HttpBody(_, body), _) => Json.parse[T](body.asString).map((url, _))
case _ => None
}
}
但是,一旦我添加了一个新模型(和关联的 JsonReader),代码就不再编译并出现此错误:
ambiguous implicit values:
[error] both value personFormat in object Json of type => spray.json.RootJsonFormat[com.rsslldnphy.foam.models.Person]
[error] and value animalFormat in object Json of type => spray.json.RootJsonFormat[com.rsslldnphy.foam.models.Animal]
[error] match expected type spray.json.JsonReader[T]
[error] case Post (Routes.person.post, p: Person) => sender ! Ok(Actions.person.post(p))
JsonReader 的泛型类型不同的事实似乎已丢失。这种类型的擦除吗?有没有办法得到我想要的?
这是迄今为止项目的完整编译代码,ExampleService 中的注释解释了导致它崩溃的原因:github.com/rsslldnphy/foam。感谢您的帮助,谢谢。
或者如果我想要的目前无法实现,任何人都可以提出替代方法吗?
【问题讨论】:
-
你试过
case Post[Person]吗? -
我得到:
not found: type Post。我没有定义一个类,只是一个对象。 -
也许如果你想在答案中提供可编译的代码,你应该在问题中提供相同的内容
-
我不是在要求可编译的代码,只是解释你的意思以及为什么它应该工作!但如果有帮助,我会将整个项目上传到 github。
-
@KimStebel 我已经为问题添加了 github 链接。感谢您抽出宝贵时间。
标签: scala generics typeclass spray-json