Skip to content

Commit e54b271

Browse files
committed
Add missing support of mutable.CollisionProofHashMap
1 parent c85027a commit e54b271

File tree

4 files changed

+32
-6
lines changed

4 files changed

+32
-6
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,9 @@ object JsonCodecMaker {
694694
def isMutableArraySeq(tpe: Type): Boolean =
695695
isScala213 && tpe.typeSymbol.fullName == "scala.collection.mutable.ArraySeq"
696696

697+
def isCollisionProofHashMap(tpe: Type): Boolean =
698+
isScala213 && tpe.typeSymbol.fullName == "scala.collection.mutable.CollisionProofHashMap"
699+
697700
def inferImplicitValue(typeTree: Tree): Tree = c.inferImplicitValue(c.typecheck(typeTree, c.TYPEmode).tpe)
698701

699702
def checkRecursionInTypes(types: List[Type]): Unit =
@@ -1612,7 +1615,7 @@ object JsonCodecMaker {
16121615
genReadMapAsArray(newBuilder,
16131616
q"x = x.updated($readKey, { if (in.isNextToken(',')) $readVal else in.commaError() })")
16141617
} else genReadMap(newBuilder, q"x = x.updated(in.readKeyAsLong(), $readVal)")
1615-
} else if (tpe <:< typeOf[mutable.Map[_, _]]) withDecoderFor(methodKey, default) {
1618+
} else if (tpe <:< typeOf[mutable.Map[_, _]] || isCollisionProofHashMap(tpe)) withDecoderFor(methodKey, default) {
16161619
val tpe1 = typeArg1(tpe)
16171620
val tpe2 = typeArg2(tpe)
16181621
val newBuilder = q"{ val x = if (default.isEmpty) default else ${scalaCollectionCompanion(tpe)}.empty[$tpe1, $tpe2] }"
@@ -2045,7 +2048,7 @@ object JsonCodecMaker {
20452048
genWriteMapAsArray(q"x", writeVal1, writeVal2)
20462049
} else genWriteMap(q"x", q"out.writeKey(kv._1)", writeVal2)
20472050
}
2048-
} else if (tpe <:< typeOf[collection.Map[_, _]]) withEncoderFor(methodKey, m) {
2051+
} else if (tpe <:< typeOf[collection.Map[_, _]] || isCollisionProofHashMap(tpe)) withEncoderFor(methodKey, m) {
20492052
val tpe1 = typeArg1(tpe)
20502053
val tpe2 = typeArg2(tpe)
20512054
if (isScala213) {

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1705,7 +1705,8 @@ 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[collection.SortedMap[_, _]]) withNullValueFor(tpe) {
1708+
} else if (tpe <:< TypeRepr.of[collection.SortedMap[_, _]] ||
1709+
tpe <:< TypeRepr.of[mutable.CollisionProofHashMap[_, _]]) withNullValueFor(tpe) {
17091710
val tpe1 = typeArg1(tpe)
17101711
Apply(scalaMapEmptyNoArgs(tpe, tpe1, typeArg2(tpe)), List(summonOrdering(tpe1))).asExprOf[T]
17111712
} else if (tpe <:< TypeRepr.of[immutable.Map[_, _]]) withNullValueFor(tpe) {
@@ -2310,14 +2311,15 @@ object JsonCodecMaker {
23102311
genReadMap(newBuilder, x => Assign(x.asTerm, '{ $x.updated($in.readKeyAsLong(), $readVal) }.asTerm).asExprOf[Unit],
23112312
identity, in, default.asExprOf[immutable.LongMap[t1]]).asExprOf[T]
23122313
}
2313-
} else if (tpe <:< TypeRepr.of[mutable.Map[_, _]]) withDecoderFor(methodKey, default, in) { (in, default) =>
2314+
} else if (tpe <:< TypeRepr.of[mutable.Map[_, _]] ||
2315+
tpe <:< TypeRepr.of[mutable.CollisionProofHashMap[_, _]]) withDecoderFor(methodKey, default, in) { (in, default) =>
23142316
val tpe1 = typeArg1(tpe)
23152317
val tpe2 = typeArg2(tpe)
23162318
(tpe1.asType, tpe2.asType) match
23172319
case ('[t1], '[t2]) =>
23182320
val tDefault = default.asExprOf[T & mutable.Map[t1, t2]]
23192321
val tEmpty =
2320-
if (tpe <:< TypeRepr.of[mutable.SortedMap[_, _]]) {
2322+
if (tpe <:< TypeRepr.of[mutable.SortedMap[_, _]] || tpe <:< TypeRepr.of[mutable.CollisionProofHashMap[_, _]]) {
23212323
Apply(scalaMapEmptyNoArgs(tpe, tpe1, tpe2), List(summonOrdering(tpe1))).asExprOf[T & mutable.Map[t1, t2]]
23222324
} else scalaMapEmptyNoArgs(tpe, tpe1, tpe2).asExprOf[T & mutable.Map[t1, t2]]
23232325
val newBuilder = '{
@@ -2911,7 +2913,8 @@ object JsonCodecMaker {
29112913
genWriteMapAsArrayScala213(tx, writeVal1, writeVal2, out)
29122914
} else genWriteMapScala213(tx, (out, k) => '{ $out.writeKey($k) }, writeVal2, out)
29132915
}
2914-
} else if (tpe <:< TypeRepr.of[collection.Map[_, _]]) withEncoderFor(methodKey, m, out) { (out, x) =>
2916+
} else if (tpe <:< TypeRepr.of[collection.Map[_, _]] ||
2917+
tpe <:< TypeRepr.of[mutable.CollisionProofHashMap[_, _]]) withEncoderFor(methodKey, m, out) { (out, x) =>
29152918
val tpe1 = typeArg1(tpe)
29162919
val tpe2 = typeArg2(tpe)
29172920
(tpe1.asType, tpe2.asType) match
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.github.plokhotnyuk.jsoniter_scala.macros
2+
3+
import com.github.plokhotnyuk.jsoniter_scala.macros.JsonCodecMaker._
4+
5+
class JsonCodecMakerNewTypeSpec extends VerifyingSpec {
6+
import NamespacePollutions._
7+
8+
"JsonCodecMaker.make generate codecs which" should {
9+
"serialize and deserialize new collection types" in {
10+
verifySerDeser(make[_root_.scala.collection.mutable.CollisionProofHashMap[String, Int]],
11+
_root_.scala.collection.mutable.CollisionProofHashMap[String, Int]("WWW" -> 1, "VVV" -> 2),
12+
"""{"WWW":1,"VVV":2}""")
13+
}
14+
}
15+
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ class JsonCodecMakerNewTypeSpec extends VerifyingSpec {
7171
verifySer(make[IArrayDefaults](CodecMakerConfig.withTransientEmpty(false)),
7272
IArrayDefaults(aa = IArray[IArray[Int]](), a = IArray[BigInt]()), """{"aa":[],"a":[]}""")
7373
}
74+
"serialize and deserialize new collection types" in {
75+
verifySerDeser(make[collection.mutable.CollisionProofHashMap[String, Int]],
76+
collection.mutable.CollisionProofHashMap[String, Int]("WWW" -> 1, "VVV" -> 2),
77+
"""{"WWW":1,"VVV":2}""")
78+
}
7479
"don't generate codecs for union types with proper compilation error" in {
7580
assert(intercept[TestFailedException](assertCompiles {
7681
"""type ABC = "A" | "B" | "C"

0 commit comments

Comments
 (0)