For this case class:
case class People(names: Set[Int])
Travis Brown explained how to create PeopleReads: Reads[People]
at this answer:
implicit val PeopleReads =
(__ "names").read[Set[Id]].map(People)
But, I'm trying to implement PeopleWrites: Writes[People]
:
implicit val PeopleWrites: Writes[People] =
(JsPath "names").write[Set[Int]].map(unlift(x => Some((x.names)))
with the following compile-time error:
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)))
How can I resolve this error?
Also, how can I write Format[People]
where I get/define both Reads
and Writes
:
val peopleFormat: Format[People] = ...
?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…