Skip to content

Commit 726f6aa

Browse files
committed
Add missing support of ordering when reading SortedMap and TreeMap collections with Scala 3
1 parent 4b4f897 commit 726f6aa

File tree

2 files changed

+43
-9
lines changed

2 files changed

+43
-9
lines changed

jsoniter-scala-macros/shared/src/main/scala-3/com/github/plokhotnyuk/jsoniter_scala/macros/JsonCodecMaker.scala

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1705,7 +1705,7 @@ object JsonCodecMaker {
17051705
tpe <:< TypeRepr.of[mutable.LongMap[_]] || tpe <:< TypeRepr.of[immutable.Seq[_]] ||
17061706
tpe <:< TypeRepr.of[Set[_]]) withNullValueFor(tpe) {
17071707
scalaCollectionEmptyNoArgs(tpe, typeArg1(tpe)).asExprOf[T]
1708-
} else if (tpe <:< TypeRepr.of[immutable.SortedMap[_, _]]) withNullValueFor(tpe) {
1708+
} else if (tpe <:< TypeRepr.of[collection.SortedMap[_, _]]) withNullValueFor(tpe) {
17091709
val tpe1 = typeArg1(tpe)
17101710
Apply(scalaMapEmptyNoArgs(tpe, tpe1, typeArg2(tpe)), List(summonOrdering(tpe1))).asExprOf[T]
17111711
} else if (tpe <:< TypeRepr.of[immutable.Map[_, _]]) withNullValueFor(tpe) {
@@ -2322,9 +2322,13 @@ object JsonCodecMaker {
23222322
(tpe1.asType, tpe2.asType) match
23232323
case ('[t1], '[t2]) =>
23242324
val tDefault = default.asExprOf[T & mutable.Map[t1, t2]]
2325+
val tEmpty =
2326+
if (tpe <:< TypeRepr.of[mutable.SortedMap[_, _]]) {
2327+
Apply(scalaMapEmptyNoArgs(tpe, tpe1, tpe2), List(summonOrdering(tpe1))).asExprOf[T & mutable.Map[t1, t2]]
2328+
} else scalaMapEmptyNoArgs(tpe, tpe1, tpe2).asExprOf[T & mutable.Map[t1, t2]]
23252329
val newBuilder = '{
23262330
if ($tDefault.isEmpty) $tDefault
2327-
else ${scalaMapEmptyNoArgs(tpe, tpe1, tpe2).asExprOf[T & mutable.Map[t1, t2]]}
2331+
else $tEmpty
23282332
}.asExprOf[T & mutable.Map[t1, t2]]
23292333

23302334
def readVal2(using Quotes) =
@@ -2347,7 +2351,7 @@ object JsonCodecMaker {
23472351
val builderNoApply =
23482352
TypeApply(Select.unique(scalaCollectionCompanion(tpe), "newBuilder"), List(TypeTree.of[t1], TypeTree.of[t2]))
23492353
val newBuilder =
2350-
(if (tpe <:< TypeRepr.of[immutable.SortedMap[_, _]]) Apply(builderNoApply, List(summonOrdering(tpe1))) // TODO: add withOrderfingFoe
2354+
(if (tpe <:< TypeRepr.of[collection.SortedMap[_, _]]) Apply(builderNoApply, List(summonOrdering(tpe1)))
23512355
else builderNoApply).asExprOf[mutable.Builder[(t1, t2), T & collection.Map[t1, t2]]]
23522356

23532357
def readVal2(using Quotes) =

jsoniter-scala-macros/shared/src/test/scala/com/github/plokhotnyuk/jsoniter_scala/macros/JsonCodecMakerSpec.scala

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -549,11 +549,26 @@ class JsonCodecMakerSpec extends VerifyingSpec {
549549
implicit val levelOrdering: Ordering[Level] = new Ordering[Level] {
550550
override def compare(x: Level, y: Level): Int = y.ordinal - x.ordinal
551551
}
552-
val codecOfOrderedLevelTreeMap = make[_root_.scala.collection.immutable.TreeMap[Level, Int]]
553-
verifySerDeser(codecOfOrderedLevelTreeMap,
552+
verifySerDeser(make[_root_.scala.collection.immutable.TreeMap[Level, Int]],
554553
_root_.scala.collection.immutable.TreeMap[Level, Int](Level.HIGH -> 100, Level.LOW -> 10), """{"0":10,"1":100}""")
555-
verifyDeserByCheck(codecOfOrderedLevelTreeMap, """{"0":10,"1":100}""",
554+
verifyDeserByCheck(make[_root_.scala.collection.immutable.TreeMap[Level, Int]], """{"0":10,"1":100}""",
556555
check = (actual: _root_.scala.collection.immutable.TreeMap[Level, Int]) => actual.ordering shouldBe levelOrdering)
556+
verifySerDeser(make[_root_.scala.collection.immutable.SortedMap[Level, Int]],
557+
_root_.scala.collection.immutable.SortedMap[Level, Int](Level.HIGH -> 100, Level.LOW -> 10), """{"0":10,"1":100}""")
558+
verifyDeserByCheck(make[_root_.scala.collection.immutable.SortedMap[Level, Int]], """{"0":10,"1":100}""",
559+
check = (actual: _root_.scala.collection.immutable.SortedMap[Level, Int]) => actual.ordering shouldBe levelOrdering)
560+
verifySerDeser(make[_root_.scala.collection.mutable.TreeMap[Level, Int]],
561+
_root_.scala.collection.mutable.TreeMap[Level, Int](Level.HIGH -> 100, Level.LOW -> 10), """{"0":10,"1":100}""")
562+
verifyDeserByCheck(make[_root_.scala.collection.mutable.TreeMap[Level, Int]], """{"0":10,"1":100}""",
563+
check = (actual: _root_.scala.collection.mutable.TreeMap[Level, Int]) => actual.ordering shouldBe levelOrdering)
564+
verifySerDeser(make[_root_.scala.collection.mutable.SortedMap[Level, Int]],
565+
_root_.scala.collection.mutable.SortedMap[Level, Int](Level.HIGH -> 100, Level.LOW -> 10), """{"0":10,"1":100}""")
566+
verifyDeserByCheck(make[_root_.scala.collection.mutable.SortedMap[Level, Int]], """{"0":10,"1":100}""",
567+
check = (actual: _root_.scala.collection.mutable.SortedMap[Level, Int]) => actual.ordering shouldBe levelOrdering)
568+
verifySerDeser(make[_root_.scala.collection.SortedMap[Level, Int]],
569+
_root_.scala.collection.SortedMap[Level, Int](Level.HIGH -> 100, Level.LOW -> 10), """{"0":10,"1":100}""")
570+
verifyDeserByCheck(make[_root_.scala.collection.SortedMap[Level, Int]], """{"0":10,"1":100}""",
571+
check = (actual: _root_.scala.collection.SortedMap[Level, Int]) => actual.ordering shouldBe levelOrdering)
557572
}
558573
"serialize and deserialize types using a custom value codec and a custom ordering for set values" in {
559574
implicit val codecOfLevel: JsonValueCodec[Level] = new JsonValueCodec[Level] {
@@ -574,11 +589,26 @@ class JsonCodecMakerSpec extends VerifyingSpec {
574589
implicit val levelOrdering: Ordering[Level] = new Ordering[Level] {
575590
override def compare(x: Level, y: Level): Int = y.ordinal - x.ordinal
576591
}
577-
val codecOfOrderedLevelTreeSet = make[_root_.scala.collection.immutable.TreeSet[Level]]
578-
verifySerDeser(codecOfOrderedLevelTreeSet,
592+
verifySerDeser(make[_root_.scala.collection.immutable.TreeSet[Level]],
579593
_root_.scala.collection.immutable.TreeSet[Level](Level.HIGH, Level.LOW), """[0,1]""")
580-
verifyDeserByCheck(codecOfOrderedLevelTreeSet, """[0,1]""",
594+
verifyDeserByCheck(make[_root_.scala.collection.immutable.TreeSet[Level]], """[0,1]""",
581595
check = (actual: _root_.scala.collection.immutable.TreeSet[Level]) => actual.ordering shouldBe levelOrdering)
596+
verifySerDeser(make[_root_.scala.collection.immutable.SortedSet[Level]],
597+
_root_.scala.collection.immutable.SortedSet[Level](Level.HIGH, Level.LOW), """[0,1]""")
598+
verifyDeserByCheck(make[_root_.scala.collection.immutable.SortedSet[Level]], """[0,1]""",
599+
check = (actual: _root_.scala.collection.immutable.SortedSet[Level]) => actual.ordering shouldBe levelOrdering)
600+
verifySerDeser(make[_root_.scala.collection.mutable.TreeSet[Level]],
601+
_root_.scala.collection.mutable.TreeSet[Level](Level.HIGH, Level.LOW), """[0,1]""")
602+
verifyDeserByCheck(make[_root_.scala.collection.mutable.TreeSet[Level]], """[0,1]""",
603+
check = (actual: _root_.scala.collection.mutable.TreeSet[Level]) => actual.ordering shouldBe levelOrdering)
604+
verifySerDeser(make[_root_.scala.collection.mutable.SortedSet[Level]],
605+
_root_.scala.collection.mutable.SortedSet[Level](Level.HIGH, Level.LOW), """[0,1]""")
606+
verifyDeserByCheck(make[_root_.scala.collection.mutable.SortedSet[Level]], """[0,1]""",
607+
check = (actual: _root_.scala.collection.mutable.SortedSet[Level]) => actual.ordering shouldBe levelOrdering)
608+
verifySerDeser(make[_root_.scala.collection.SortedSet[Level]],
609+
_root_.scala.collection.SortedSet[Level](Level.HIGH, Level.LOW), """[0,1]""")
610+
verifyDeserByCheck(make[_root_.scala.collection.SortedSet[Level]], """[0,1]""",
611+
check = (actual: _root_.scala.collection.SortedSet[Level]) => actual.ordering shouldBe levelOrdering)
582612
}
583613
"serialize and deserialize enumerations" in {
584614
verifySerDeser(codecOfEnums1, Enums(LocationType.GPS), """{"lt":"GPS"}""")

0 commit comments

Comments
 (0)