Skip to content

Commit

Permalink
KeyWrites for AnyVal
Browse files Browse the repository at this point in the history
  • Loading branch information
cchantep committed Mar 1, 2021
1 parent 801b53b commit 8e5617a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ object KeyReads extends EnvKeyReads with LowPriorityKeyReads {
implicit val charKeyReads: KeyReads[Char] = KeyReads[Char] {
_.headOption match {
case Some(ch) => JsSuccess(ch)
case _ => JsError("error.expected.character")
case _ => JsError("error.expected.character")
}
}

implicit val booleanKeyReads: KeyReads[Boolean] = KeyReads[Boolean] {
case "true" => JsSuccess(true)
case "true" => JsSuccess(true)
case "false" => JsSuccess(false)
case _ => JsError("error.expected.boolean")
case _ => JsError("error.expected.boolean")
}

implicit val byteKeyReads: KeyReads[Byte] = charKeyReads.map(_.toByte)
Expand All @@ -58,7 +58,7 @@ object KeyReads extends EnvKeyReads with LowPriorityKeyReads {
implicit val doubleKeyReads: KeyReads[Double] =
unsafe[Double]("error.expected.double")(_.toDouble)

private def unsafe[T](err: String)(f: String => T): KeyReads[T] =
private def unsafe[T](err: String)(f: String => T): KeyReads[T] =
KeyReads[T] { key =>
try {
JsSuccess(f(key))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@ object KeyWrites extends EnvKeyWrites {
def apply[T](f: T => String): KeyWrites[T] = new KeyWrites[T] {
def writeKey(key: T) = f(key)
}

implicit def anyValKeyWrites[T <: AnyVal]: KeyWrites[T] =
KeyWrites[T](_.toString)
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,22 @@ final class ReadsSharedSpec extends AnyWordSpec with Matchers with Inside {

"be read with boolean keys".which {
"are booleans" in {
Json.fromJson[Map[Boolean, String]](Json.obj(
"true" -> "foo", "false" -> "bar")) mustEqual JsSuccess(
Map[Boolean, String](true -> "foo", false -> "bar"))
Json
.fromJson[Map[Boolean, String]](Json.obj("true" -> "foo", "false" -> "bar"))
.mustEqual(JsSuccess(Map[Boolean, String](true -> "foo", false -> "bar")))
}

"are not booleans" in {
Json.fromJson[Map[Boolean, String]](Json.obj("foo" -> "", "bar" -> "")).
mustEqual(JsError(
Json
.fromJson[Map[Boolean, String]](Json.obj("foo" -> "", "bar" -> ""))
.mustEqual(
JsError(
List(
(JsPath \ "foo", List(JsonValidationError("error.expected.boolean"))),
(JsPath \ "bar", List(JsonValidationError("error.expected.boolean"))))))
(JsPath \ "bar", List(JsonValidationError("error.expected.boolean")))
)
)
)
}
}

Expand Down Expand Up @@ -135,17 +140,13 @@ final class ReadsSharedSpec extends AnyWordSpec with Matchers with Inside {

"be read with float keys" in {
Json
.fromJson[Map[Float, String]](Json.obj(
"1.23" -> "foo",
"23.4" -> "bar"))
.fromJson[Map[Float, String]](Json.obj("1.23" -> "foo", "23.4" -> "bar"))
.mustEqual(JsSuccess(Map(1.23F -> "foo", 23.4F -> "bar")))
}

"be read with double keys" in {
Json
.fromJson[Map[Double, String]](Json.obj(
"1.23" -> "foo",
"23.4" -> "bar"))
.fromJson[Map[Double, String]](Json.obj("1.23" -> "foo", "23.4" -> "bar"))
.mustEqual(JsSuccess(Map(1.23D -> "foo", 23.4D -> "bar")))
}

Expand All @@ -154,8 +155,7 @@ final class ReadsSharedSpec extends AnyWordSpec with Matchers with Inside {

implicitly[KeyReads[URI]]

Json.fromJson[Map[URI, String]](Json.obj(key -> "foo")).
mustEqual(JsSuccess(Map((new URI(key)) -> "foo")))
Json.fromJson[Map[URI, String]](Json.obj(key -> "foo")).mustEqual(JsSuccess(Map((new URI(key)) -> "foo")))
}
}

Expand Down Expand Up @@ -292,7 +292,7 @@ final class ReadsSharedSpec extends AnyWordSpec with Matchers with Inside {
"be read from JsString" in {
val strRepr = "https://www.playframework.com/documentation/2.8.x/api/scala/index.html#play.api.libs.json.JsResult"

JsString(strRepr).validate[URI] mustEqual JsSuccess(new URI(strRepr))
JsString(strRepr).validate[URI].mustEqual(JsSuccess(new URI(strRepr)))
}

"not be read from invalid JsString" in {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ final class WritesSharedSpec extends AnyWordSpec with Matchers {

"Iterable writes" should {
"write maps" in {
Json.toJson(Map(1 -> "one")).mustEqual(Json.arr(Json.arr(1, "one")))
Json.toJson(Map(1 -> "one")).mustEqual(Json.obj("1" -> "one"))
}
}

Expand Down

0 comments on commit 8e5617a

Please sign in to comment.