Skip to content

Commit

Permalink
Add test for constructing Writes with case class
Browse files Browse the repository at this point in the history
Updated syntax in readme to support Scala 3
  • Loading branch information
Donovan Levinson committed May 13, 2024
1 parent 51bd5d6 commit 1f5807a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,10 @@ import play.api.libs.functional.syntax._
implicit val locationWrites: Writes[Location] = (
(JsPath \ "lat").write[Double] and
(JsPath \ "long").write[Double]
)(unlift(Location.unapply))
)(location => {
val Location(lat, long) = location
(lat, long)
})

implicit val residentWrites: Writes[Resident] = (
(JsPath \ "name").write[String] and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,31 @@ final class WritesSharedSpec extends AnyWordSpec with Matchers {
success[JsValue, Writes](JsString("foo"))
}

"Constructing Writes" should {
"support case class" in {
import play.api.libs.functional.syntax._

implicit val locationReads: Reads[Location] = (
(JsPath \ "lat").read[Double] and
(JsPath \ "long").read[Double]
)(Location.apply _)

implicit val locationWrites: Writes[Location] = (
(JsPath \ "lat").write[Double] and
(JsPath \ "long").write[Double]
)(location => {
val Location(lat, long) = location
(lat, long)
})

val location = Location(1.1, 2.2)

val serialized = Json.stringify(Json.toJson(location))
serialized.mustEqual("""{"lat":1.1,"long":2.2}""")
Json.fromJson[Location](Json.parse(serialized)).mustEqual(JsSuccess(location))
}
}

// ---

case class Location(lat: Double, long: Double)
Expand Down

0 comments on commit 1f5807a

Please sign in to comment.