Skip to content

Commit 256f9e0

Browse files
authored
Add implicit owritesMap method and make writes map not implicit (#37)
- Add unit test for owritesMap and backwards compatibility
1 parent be3a534 commit 256f9e0

File tree

4 files changed

+18
-7
lines changed

4 files changed

+18
-7
lines changed

build.sbt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ name := "play-json-ops-root"
44
ThisBuild / organization := "com.rallyhealth"
55
ThisBuild / organizationName := "Rally Health"
66

7-
ThisBuild / gitVersioningSnapshotLowerBound := "4.0.0"
8-
97
ThisBuild / bintrayOrganization := Some("rallyhealth")
108
ThisBuild / bintrayRepository := "maven"
119

play-json-ops-common/src/main/scala/play/api/libs/json/ops/v4/JsonImplicits.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,12 @@ private[ops] class JsonImplicits private[ops] extends ImplicitTupleFormats with
3232
}
3333
}
3434

35-
implicit def writesMap[K: WritesKey, V: Writes]: Writes[Map[K, V]] = {
35+
@deprecated("Use owritesMap instead.", "4.2.0")
36+
def writesMap[K: WritesKey, V: Writes]: Writes[Map[K, V]] = owritesMap[K, V]
37+
38+
implicit def owritesMap[K: WritesKey, V: Writes]: OWrites[Map[K, V]] = {
3639
val writesK = WritesKey.of[K]
3740
val stringKeyWriter = Writes.map[V]
38-
Writes[Map[K, V]](values => stringKeyWriter.writes(values.map { case (k, v) => (writesK.write(k), v) }))
41+
OWrites[Map[K, V]](values => stringKeyWriter.writes(values.map { case (k, v) => (writesK.write(k), v) }))
3942
}
4043
}

play-json-tests-common/src/test/scala/play/api/libs/json/ops/v4/JsonImplicitsSpec.scala

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package play.api.libs.json.ops.v4
33
import org.scalacheck.{Arbitrary, Gen, Shrink}
44
import org.scalacheck.ops._
55
import org.scalatest.FreeSpec
6-
import play.api.libs.json.{Format, Json}
6+
import play.api.libs.json.{Format, Json, OWrites, Writes}
77
import org.scalatest.prop.GeneratorDrivenPropertyChecks._
88

99
case class KeyWrapper(key: String)
@@ -59,4 +59,12 @@ class JsonImplicitsSpec extends FreeSpec {
5959
assertResult(keyWrappedMap)(parsedMap)
6060
}
6161
}
62+
63+
"implicit Writes[Map[Int, String]] should be in scope" in {
64+
implicitly[Writes[Map[Int, String]]]
65+
}
66+
67+
"implicit OWrites[Map[Int, String]] should be in scope" in {
68+
implicitly[OWrites[Map[Int, String]]]
69+
}
6270
}

play27-json-ops-scala213/src/main/scala/play/api/libs/json/ops/v4/JsonImplicits.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ private[ops] class JsonImplicits private[ops] extends ImplicitTupleFormats with
3232
}
3333
}
3434

35-
implicit def writesMap[K: WritesKey, V: Writes]: Writes[Map[K, V]] = {
35+
def writesMap[K: WritesKey, V: Writes]: Writes[Map[K, V]] = owritesMap[K, V]
36+
37+
implicit def owritesMap[K: WritesKey, V: Writes]: OWrites[Map[K, V]] = {
3638
val writesK = WritesKey.of[K]
3739
val stringKeyWriter = Writes.map[V]
38-
Writes[Map[K, V]](values => stringKeyWriter.writes(values.map { case (k, v) => (writesK.write(k), v) }))
40+
OWrites[Map[K, V]](values => stringKeyWriter.writes(values.map { case (k, v) => (writesK.write(k), v) }))
3941
}
4042
}

0 commit comments

Comments
 (0)