JSON 在 Play 应用程序中写入转换器不接受类型
JSON writes converter in Play application doesn't accept type
我有一个 Play 框架应用程序,想要创建一个 Writes
转换器,以便将某些模型转换为 JSON。我目前拥有的:
implicit val locationWrites = new Writes[Board] {
def writes(board: Board) = Json.obj(
"gameSize" -> board.n,
"fields" -> Seq(FieldWithCoords((1,2,3), "field", true)) // error for this line
)
}
implicit val fieldWrites = new Writes[FieldWithCoords] {
def writes(field: FieldWithCoords) = Json.obj(
"coords" -> field.coords.toString,
"player" -> field.p,
"isHighlighted" -> field.highlighted
)
}
不幸的是,我得到了错误:
type mismatch; found: Seq[util.FieldWithCoords] required:
play.api.libs.json.Json.JsValueWrapper Note: implicit value
locationWrites is not applicable here because it comes after the
application point and it lacks an explicit result type
如您所见,FieldWithCoords 有一个转换。我该如何解决这个问题?感谢任何帮助,谢谢。
编辑:
"fields" 稍后应该包含多个项目,因此 Seq
编译器需要将您的 Seq[FieldWithCoords]
隐式转换为 JsValueWrapper
。我能够通过提供显式结果类型来编译它,从而为它提供隐含函数启动所需的额外推动力。
implicit val fieldWrites: Writes[FieldWithCoords] = new Writes[FieldWithCoords] {
def writes(field: FieldWithCoords) = Json.obj(
"coords" -> field.coords.toString,
"player" -> field.p,
"isHighlighted" -> field.highlighted
)
}
implicit val locationWrites: Writes[Board] = new Writes[Board] {
def writes(board: Board) = Json.obj(
"gameSize" -> board.n,
"fields" -> Seq(FieldWithCoords((1,2,3), "field", true)) // error for this line
)
}
使用写入组合器被认为是比直接创建 JSON 更好的做法:
implicit lazy val fieldWrites: Writes[FieldWithCoords] = (
(__ \ "coords").write[String] and
(__ \ "player").write[String] and
(__ \ "isHighlighted").write[Boolean]
).apply(field => (field.coords.toString, field.p, field.highlighted))
implicit lazy val locationWrites: Writes[Board] = (
(__ \ "gameSize").write[Int] and
(__ \ "fields").write[Seq[FieldWithCoords]]
).apply(board => (board.n, Seq(FieldWithCoords((1,2,3), "field", true))
我有一个 Play 框架应用程序,想要创建一个 Writes
转换器,以便将某些模型转换为 JSON。我目前拥有的:
implicit val locationWrites = new Writes[Board] {
def writes(board: Board) = Json.obj(
"gameSize" -> board.n,
"fields" -> Seq(FieldWithCoords((1,2,3), "field", true)) // error for this line
)
}
implicit val fieldWrites = new Writes[FieldWithCoords] {
def writes(field: FieldWithCoords) = Json.obj(
"coords" -> field.coords.toString,
"player" -> field.p,
"isHighlighted" -> field.highlighted
)
}
不幸的是,我得到了错误:
type mismatch; found: Seq[util.FieldWithCoords] required: play.api.libs.json.Json.JsValueWrapper Note: implicit value locationWrites is not applicable here because it comes after the application point and it lacks an explicit result type
如您所见,FieldWithCoords 有一个转换。我该如何解决这个问题?感谢任何帮助,谢谢。
编辑:
"fields" 稍后应该包含多个项目,因此 Seq
编译器需要将您的 Seq[FieldWithCoords]
隐式转换为 JsValueWrapper
。我能够通过提供显式结果类型来编译它,从而为它提供隐含函数启动所需的额外推动力。
implicit val fieldWrites: Writes[FieldWithCoords] = new Writes[FieldWithCoords] {
def writes(field: FieldWithCoords) = Json.obj(
"coords" -> field.coords.toString,
"player" -> field.p,
"isHighlighted" -> field.highlighted
)
}
implicit val locationWrites: Writes[Board] = new Writes[Board] {
def writes(board: Board) = Json.obj(
"gameSize" -> board.n,
"fields" -> Seq(FieldWithCoords((1,2,3), "field", true)) // error for this line
)
}
使用写入组合器被认为是比直接创建 JSON 更好的做法:
implicit lazy val fieldWrites: Writes[FieldWithCoords] = (
(__ \ "coords").write[String] and
(__ \ "player").write[String] and
(__ \ "isHighlighted").write[Boolean]
).apply(field => (field.coords.toString, field.p, field.highlighted))
implicit lazy val locationWrites: Writes[Board] = (
(__ \ "gameSize").write[Int] and
(__ \ "fields").write[Seq[FieldWithCoords]]
).apply(board => (board.n, Seq(FieldWithCoords((1,2,3), "field", true))