我试图实现basically the same thing that is discussed here,但在我的特定情况下,它不起作用。将Scala类型传递给函数
目前我有一个函数验证来自服务器的JSON响应。问题是,它有硬编码到方法的JSON类型:
def fakeRequest[A: Writes](target:() => Call, requestObject: A): Any = {
route(FakeRequest(target()).withJsonBody(Json.toJson(requestObject))) match {
// ... stuff happens
Json.parse(contentAsString(response)).validate[GPInviteResponse]
^
注意硬编码GPInviteResponse类型。
所以,为了使它成为一个完全通用且可重用的方法,最好传入正在验证的类型。
我尝试这样做:
def fakeRequest[A: Writes, B](target:() => Call, requestObject: A, responseType: B): Any = {
route(FakeRequest(target()).withJsonBody(Json.toJson(requestObject))) match {
// ... stuff happens
Json.parse(contentAsString(response)).validate[B]
^
,几乎工作,但我得到一个No Json deserializer found for type B.
有道理,所以我把它改为:
def fakeRequest[A: Writes, B: Reads](target:() => Call, requestObject: A, responseType: B): Any = {
不过,现在我得到No Json deserializer found for type controllers.GPInviteResponse.type.
所以,问题是:是否可以传递这样的类型(或者是否有其他魔法可以使其工作)?
有的类型定义的解串器......我重新读了一半了十几次,以确保没有错字:
case class GPInviteResponse(inviteSent: Boolean, URL: Option[String], error: Option[GPRequestError] = None) {
def this(error: GPRequestError) = this(false, None, Option(error))
}
object GPInviteResponse {
implicit val readsInviteResponse = Json.reads[GPInviteResponse]
implicit val writesInviteResponse = Json.writes[GPInviteResponse]
}
编辑
包括下面是一个简化的测试案例,演示了这个问题。目前,这不会编译(错误如下所示)。我想我明白为什么它不起作用(隐约),但我不知道解决方案。我的理论为什么它不起作用:虽然提供的类型GPInviteRequest
确实有隐式读/写方法,但Scala无法建立实例B
实际上是GPInviteRequest
的连接,因此它得出结论认为B
没有读/写。
case class GPInviteResponse(inviteSent: Boolean)
object GPInviteResponse {
implicit val readsInviteResponse = Json.reads[GPInviteResponse]
implicit val writesInviteResponse = Json.writes[GPInviteResponse]
}
class TestInviteServices extends PlaySpecification {
"try to validate a type" in {
tryToValidate(GPInviteRequest(true))
}
def tryToValidate[B: Reads, Writes](i: B) = {
val json = Json.toJson(i).toString
Json.parse(json).validate[B].isSuccess must beTrue
}
}
上述试验得到:
[错误] /Users/zbeckman/Projects/Glimpulse/Server-2/project/glimpulse-server/test/application/TestInviteServices.scala:46 : 未找到B类型的Json序列化程序。尝试实施此类型的隐式 写入或格式。 [error] val json = Json.toJson(i).toString [error]^[error] /Users/zbeckman/Projects/Glimpulse/Server-2/project/glimpulse-server/test/application/TestInviteServices.scala: 133: 找不到类型controllers.GPInviteResponse.type的Json解串器。 尝试实现此类型的隐式读取或格式。 [error] fakeRequest(controllers.routes.GPInviteService。邀请我, GPInviteResponse)匹配{[错误]^
你能给出一个编译和展示问题的简单例子吗? –