scala Play2 找不到我的隐式读取或 JSON 格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10488950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Play2 does not find my implicit Reads or Format for JSON
提问by Somatik
This is my Search Object:
这是我的搜索对象:
package models.helper
import play.api.libs.json.Format
import play.api.libs.json.JsValue
import play.api.libs.json.JsObject
import play.api.libs.json.JsString
case class Search (name: String, `type`:String){
implicit object SearchFormat extends Format[Search] {
def reads(json: JsValue): Search = Search(
(json \ "name").as[String],
(json \ "type").as[String]
)
def writes(s: Search): JsValue = JsObject(Seq(
"name" -> JsString(s.name),
"type" -> JsString(s.`type`)
))
}
}
I'm trying ot use this class when calling a webservice using WS:
在使用 WS 调用 Web 服务时,我正在尝试使用此类:
val search = response.json.as[Search]
But the scala compiler keeps complaining on this line:
但是 Scala 编译器一直在这一行抱怨:
No Json deserializer found for type models.helper.Search. Try to implement an implicit Reads or Format for this type.
找不到类型为 models.helper.Search 的 Json 反序列化器。尝试为这种类型实现隐式读取或格式。
Could anybody tell me what I'm doing wrong?
谁能告诉我我做错了什么?
- got the example from https://sites.google.com/site/play20zh/scala-developers/working-with-json
- this thread discusses the same issue but gives no solution, what example on what site? https://groups.google.com/forum/?fromgroups#!topic/play-framework/WTZrmQi5XxY
- 从https://sites.google.com/site/play20zh/scala-developers/working-with-json得到示例
- 这个线程讨论了同样的问题,但没有给出解决方案,在什么网站上有什么例子?https://groups.google.com/forum/?fromgroups#!topic/play-framework/WTZrmQi5XxY
回答by Julien Richard-Foy
Indeed the example is wrong. You need your implicit Format[Search]value to be available in the implicit scope.
这个例子确实是错误的。您需要在隐Format[Search]式范围内提供隐式值。
In your case the Format[Search]is defined as a nested value of the class Search, so you can reach it only from an instance of Search.
在您的情况下Format[Search]被定义为类的嵌套值Search,因此您只能从 的实例访问它Search。
So, what you want to do is to define it in another place, where it could be referenced without having to create an instance of Search, e.g. in a Formatsobject:
所以,你想要做的是在另一个地方定义它,在那里它可以被引用而不必创建 的实例Search,例如在一个Formats对象中:
object Formats {
implicit SearchFormat extends Format[Search] {
…
}
}
Then you can use it as follows:
然后您可以按如下方式使用它:
import Formats.SearchFormat
val search = response.json.as[Search]
You can also get rid of the import taxby defining the Format[Search]value in the companion object of the Searchclass. Indeed the Scala compiler automatically looks in companion objects of type parameters when it needs an implicit value of a given type:
您还可以通过在类的伴随对象中定义值来免除进口税。实际上,当 Scala 编译器需要给定类型的隐式值时,它会自动查找类型参数的伴随对象:Format[Search]Search
case class Search(name: String, `type`: String)
object Search {
implicit object SearchFormat extends Format[Search] {
…
}
}
Then you can use it without having to import it:
然后你可以使用它而不必导入它:
val search = response.json.as[Search]

