gpt4 book ai didi

scala - Play Framework 2.2.1 Json Combinators - 读取和写入编译错误

转载 作者:行者123 更新时间:2023-12-01 12:45:09 26 4
gpt4 key购买 nike

尝试运行来自 http://www.playframework.com/documentation/2.2.x/ScalaJsonCombinators 的 Json 读取和写入的 Json Combinator 示例:

def test = Action {

case class Creature(name: String,isDead: Boolean,weight: Float, email: String, favorites: (String, Int), friends: List[Creature] = Nil, social: Option[String] = None)
import play.api.libs.json._
import play.api.libs.functional.syntax._

implicit val creatureWrites: Writes[Creature] = (
(__ \ "name").write[String] and
(__ \ "isDead").write[Boolean] and
(__ \ "weight").write[Float] and
(__ \ "email").write[String] and
(__ \ "favorites").write(
(__ \ "string").write[String] and
(__ \ "number").write[Int]
tupled
) and
(__ \ "friends").lazyWrite(Writes.traversableWrites[Creature](creatureWrites)) and
(__ \ "social").write[Option[String]]
)(unlift(Creature.unapply))

val gizmo = Creature("gremlins", false, 1.0F, "gizmo@midnight.com", ("alpha", 85), List(), Some("@gizmo"))
val gizmojs = Json.toJson(gizmo)

Ok(gizmojs toString)
}

我收到以下编译错误:

[error] ....:forward reference extends over definition of value creatureWrites
[error] (__ \ "friends").lazyWrite(Writes.traversableWrites[Creature](creatureWrites))

我对ReadsFormat 也有类似的问题。

请帮忙。

最佳答案

That's because you are define the implicit value as local member. Classes and objects allow forward references, whereas local block do not

So just define the implicit reads and writes as class member, not local

看我的测试

规范:

import play.api.libs.json._
import play.api.libs.functional.syntax._
import org.specs2.mutable.Specification

case class Creature(
name: String,
isDead: Boolean,
weight: Float,
email: String,
favorites: (String, Int),
friends: List[Creature] = Nil,
social: Option[String] = None)

class FooSpec extends Specification {
implicit val creatureWrites: Writes[Creature] = (
(__ \ "name").write[String] and
(__ \ "isDead").write[Boolean] and
(__ \ "weight").write[Float] and
(__ \ "email").write[String] and
(__ \ "favorites").write(
(__ \ "string").write[String] and
(__ \ "number").write[Int]
tupled
) and
(__ \ "friends").lazyWrite(Writes.traversableWrites[Creature](creatureWrites)) and
(__ \ "social").write[Option[String]]
)(unlift(Creature.unapply))
implicit val favouriteReads: Reads[(String, Int)] =
(__ \ "string").read[String]and
(__ \ "number").read[Int] tupled
implicit val creatureReads = Json.reads[Creature]

val gizmo = Creature("gremlins", false, 1.0F, "gizmo@midnight.com", ("alpha", 85), List(), Some("@gizmo"))
val gizmojs = Json.toJson(gizmo)
"writes" should {
"write scala value as string" in {
Json.parse(gizmojs.toString).as[Creature] must be_==(gizmo)
}
}
}

结果:

[success] Total time: 10 s, completed Jan 1, 2014 11:25:25 PM
[ops-ui] $ testOnly FooSpec
[info] FooSpec
[info] writes should
[info] + write scala value as string
[info] Total for specification FooSpec
[info] Finished in 1 second, 813 ms
[info] 1 example, 0 failure, 0 error
[info] Passed: Total 1, Failed 0, Errors 0, Passed 1
[success] Total time: 6 s, completed Jan 1, 2014 11:25:45 PM

您可以像这样轻松定义读/写

implicit val favouriteWrites =
(__ \ "string").write[String] and
(__ \ "number").write[Int] tupled
implicit val creatureWrites: Writes[Creature] = Json.writes[Creature]

implicit val favouriteReads: Reads[(String, Int)] =
(__ \ "string").read[String]and
(__ \ "number").read[Int] tupled
implicit val creatureReads = Json.reads[Creature]

关于scala - Play Framework 2.2.1 Json Combinators - 读取和写入编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20866164/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com