Scala:diver diver type play.api.libs.json.writes/reads的隐式扩展
我正在阅读本教程: https://www.playframework.com.com.com/documentation/2.8。 x/scalajson
1
def myAction = Action.async { request: MessagesRequest[AnyContent] =>
Future {
case class MyData(first: Int, second: Int)
implicit val myDataWrites = new Writes[MyData] {
def writes(myData: MyData) = Json.obj(
"first" -> myData.first,
"second" -> myData.second
)
}
Ok(Json.toJson("result" -> new MyData(1, 1)))
}
}
[error] diverging implicit expansion for type play.api.libs.json.Reads[MyData]
[error] starting with method GenericFormat in trait DefaultFormat
[error] Ok(Json.toJson("result" -> new MyData(1, 1)))
[error] ^
[error] one error found
。一直按照教程中的说明:
要将自己的模型转换为JSvalues,您必须定义隐式 写入转换器并在范围内提供它们。
这里缺少什么?
2。如果将代码更改为:
case class MoreData(a: Long, b: Long)
implicit val reads: Reads[MoreData] = Json.reads[MoreData]
def myAction = Action.async(parse.json[MoreData]) { request =>
Future {
case class MyData(first: Int, second: Int)
implicit val myDataWrites = new Writes[MyData] {
def writes(myData: MyData) = Json.obj(
"first" -> myData.first,
"second" -> myData.second
)
}
Ok(Json.toJson("result" -> new MyData(1, 1)))
}
}
我会收到以下汇编错误:
[error] diverging implicit expansion for type play.api.libs.json.Writes[(String, MyData)]
[error] starting with method GenericFormat in trait DefaultFormat
[error] Ok(Json.toJson("result" -> new MyData(1, 1)))
[error] ^
[error] one error found
同样,我不明白这一点。似乎在范围中有写下[mydata]
,而且似乎只有一个。那为什么不起作用呢?
为什么使用parse.json [moredata]
将错误汇编从读取
更改为 Writes ?
3。另外,我在Intellij
中编写了此代码。通常突出显示汇编错误。例如,如果我删除隐式mydatawrites
,Intellij确实突出了汇编错误。但是,有了上面的代码,它并没有突出显示任何内容。
为什么?
I am reading this tutorial: https://www.playframework.com/documentation/2.8.x/ScalaJson
1. In a Play/Scala controller I have the following:
def myAction = Action.async { request: MessagesRequest[AnyContent] =>
Future {
case class MyData(first: Int, second: Int)
implicit val myDataWrites = new Writes[MyData] {
def writes(myData: MyData) = Json.obj(
"first" -> myData.first,
"second" -> myData.second
)
}
Ok(Json.toJson("result" -> new MyData(1, 1)))
}
}
When I compile from the command line, I am getting the following error:
[error] diverging implicit expansion for type play.api.libs.json.Reads[MyData]
[error] starting with method GenericFormat in trait DefaultFormat
[error] Ok(Json.toJson("result" -> new MyData(1, 1)))
[error] ^
[error] one error found
It seems to me like I've been following the instructions in the tutorial:
To convert your own models to JsValues, you must define implicit
Writes converters and provide them in scope.
What is missing here?
2. If I change the code to:
case class MoreData(a: Long, b: Long)
implicit val reads: Reads[MoreData] = Json.reads[MoreData]
def myAction = Action.async(parse.json[MoreData]) { request =>
Future {
case class MyData(first: Int, second: Int)
implicit val myDataWrites = new Writes[MyData] {
def writes(myData: MyData) = Json.obj(
"first" -> myData.first,
"second" -> myData.second
)
}
Ok(Json.toJson("result" -> new MyData(1, 1)))
}
}
I get the following compilation error:
[error] diverging implicit expansion for type play.api.libs.json.Writes[(String, MyData)]
[error] starting with method GenericFormat in trait DefaultFormat
[error] Ok(Json.toJson("result" -> new MyData(1, 1)))
[error] ^
[error] one error found
Again, I don't understand this. It seems like there is Writes[MyData]
in the scope, and it seems like there is only one. So why doesn't it work?
And why did using parse.json[MoreData]
change the error compilation from Reads
to Writes
?
3. Also, I wrote this code in Intellij
. Which usually highlights compilation errors. For example, if I remove the implicit myDataWrites
, intellij does highlight a compilation error. But with the code above it does not highlight anything.
Why is that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论