【发布时间】:2014-02-11 22:49:21
【问题描述】:
对于这个案例类:
case class People(names: Set[Int])
Travis Brown 解释了如何在此answer 创建PeopleReads: Reads[People]:
implicit val PeopleReads =
(__ \ "names").read[Set[Id]].map(People)
但是,我正在尝试实现PeopleWrites: Writes[People]:
implicit val PeopleWrites: Writes[People] =
(JsPath \ "names").write[Set[Int]].map(unlift(x => Some((x.names)))
出现以下编译时错误:
scala> People( Set(1,2,3))
res5: People = People(Set(1, 2, 3))
scala> implicit val PeopleWrites: Writes[People] =
(JsPath \ "names").write[Set[Int]].map(unlift(x => Some((x.names))))
<console>:21: error: value map is not a member of
play.api.libs.json.OWrites[Set[Int]]
implicit val PeopleWrites: Writes[People] =
(JsPath \ "names").write[Set[Int]].
map(unlift(x => Some((x.names)))
我该如何解决这个错误?
另外,我如何编写Format[People] 来获取/定义Reads 和Writes:
val peopleFormat: Format[People] = ...?
【问题讨论】:
标签: json scala playframework