@@ -673,9 +673,17 @@ object JsonCodecMaker {
673
673
674
674
def warn (msg : String ): Unit = c.warning(c.enclosingPosition, msg)
675
675
676
- def typeArg1 (tpe : Type ): Type = tpe.typeArgs.head .dealias
676
+ def typeArgs (tpe : Type ): List [ Type ] = tpe.typeArgs.map(_ .dealias)
677
677
678
- def typeArg2 (tpe : Type ): Type = tpe.typeArgs(1 ).dealias
678
+ def typeArg1 (tpe : Type ): Type = tpe.typeArgs match {
679
+ case typeArg1 :: _ => typeArg1.dealias
680
+ case _ => fail(s " Cannot get 1st type argument in ' $tpe' " )
681
+ }
682
+
683
+ def typeArg2 (tpe : Type ): Type = tpe.typeArgs match {
684
+ case _ :: typeArg2 :: _ => typeArg2.dealias
685
+ case _ => fail(s " Cannot get 2nd type argument in ' $tpe' " )
686
+ }
679
687
680
688
val tupleSymbols : Set [Symbol ] = definitions.TupleClass .seq.toSet
681
689
@@ -690,7 +698,7 @@ object JsonCodecMaker {
690
698
if (tpe.typeSymbol.isClass) tpe.typeSymbol.asClass.typeParams
691
699
else Nil
692
700
if (tpeTypeParams.isEmpty) mtpe
693
- else mtpe.substituteTypes(tpeTypeParams, tpe. typeArgs)
701
+ else mtpe.substituteTypes(tpeTypeParams, typeArgs(tpe) )
694
702
}
695
703
696
704
def valueClassValueSymbol (tpe : Type ): MethodSymbol = tpe.decls.head.asMethod
@@ -957,11 +965,10 @@ object JsonCodecMaker {
957
965
val subTpe =
958
966
if (typeParams.isEmpty) classSymbol.toType
959
967
else {
960
- val typeParamsAndArgs = tpeClass.typeParams.map(_.toString).zip(tpe. typeArgs).toMap
961
- val typeArgs = typeParams.map(s => typeParamsAndArgs.getOrElse(s .toString, fail {
968
+ val typeParamsAndArgs = tpeClass.typeParams.map(_.toString).zip(typeArgs(tpe) ).toMap
969
+ classSymbol.toType.substituteTypes(typeParams, typeParams.map(tp => typeParamsAndArgs.getOrElse(tp .toString, fail {
962
970
s " Cannot resolve generic type(s) for ` ${classSymbol.toType}`. Please provide a custom implicitly accessible codec for it. "
963
- }))
964
- classSymbol.toType.substituteTypes(typeParams, typeArgs)
971
+ })))
965
972
}
966
973
if (isSealedClass(subTpe)) collectRecursively(subTpe)
967
974
else if (isValueClass(subTpe)) {
@@ -1687,8 +1694,9 @@ object JsonCodecMaker {
1687
1694
else if (tpe <:< typeOf[Array [_]] || isImmutableArraySeq(tpe) ||
1688
1695
isMutableArraySeq(tpe)) withDecoderFor(methodKey, default) {
1689
1696
val tpe1 = typeArg1(tpe)
1697
+ val requiresArrayCopy = tpe1.typeArgs.nonEmpty || isValueClass(tpe1)
1690
1698
val growArray =
1691
- if (tpe1.typeArgs.nonEmpty || isValueClass(tpe1) ) {
1699
+ if (requiresArrayCopy ) {
1692
1700
q """ l <<= 1
1693
1701
val x1 = new Array[ $tpe1](l)
1694
1702
_root_.java.lang.System.arraycopy(x, 0, x1, 0, i)
@@ -1698,7 +1706,7 @@ object JsonCodecMaker {
1698
1706
_root_.java.util.Arrays.copyOf(x, l) """
1699
1707
}
1700
1708
val shrinkArray =
1701
- if (tpe1.typeArgs.nonEmpty || isValueClass(tpe1) ) {
1709
+ if (requiresArrayCopy ) {
1702
1710
q """ val x1 = new Array[ $tpe1](i)
1703
1711
_root_.java.lang.System.arraycopy(x, 0, x1, 0, i)
1704
1712
x1 """
@@ -1895,19 +1903,17 @@ object JsonCodecMaker {
1895
1903
${genReadEnumValue(javaEnumValues(tpe), q " in.enumValueError(l) " )}
1896
1904
} else in.readNullOrTokenError(default, '"') """
1897
1905
} else if (isTuple(tpe)) withDecoderFor(methodKey, default) {
1898
- val indexedTypes = tpe.typeArgs
1899
- val readFields = indexedTypes.tail.foldLeft[Tree ] {
1900
- val t = typeArg1(tpe)
1901
- q " val _1: $t = ${genReadVal(t :: types, genNullValue(t :: types), isStringified, EmptyTree )}"
1902
- }{
1903
- var i = 1
1904
- (acc, ta) =>
1906
+ val indexedTypes = typeArgs(tpe)
1907
+ val readFields = indexedTypes.map {
1908
+ var i = 0
1909
+ t =>
1905
1910
i += 1
1906
- val t = ta.dealias
1907
- q """ .. $acc
1908
- val ${TermName (" _" + i)}: $t =
1909
- if (in.isNextToken(',')) ${genReadVal(t :: types, genNullValue(t :: types), isStringified, EmptyTree )}
1910
- else in.commaError() """
1911
+ if (i == 1 ) q " val _1: $t = ${genReadVal(t :: types, genNullValue(t :: types), isStringified, EmptyTree )}"
1912
+ else {
1913
+ q """ val ${TermName (" _" + i)}: $t =
1914
+ if (in.isNextToken(',')) ${genReadVal(t :: types, genNullValue(t :: types), isStringified, EmptyTree )}
1915
+ else in.commaError() """
1916
+ }
1911
1917
}
1912
1918
val params = (1 to indexedTypes.length).map(i => TermName (" _" + i))
1913
1919
q """ if (in.isNextToken('[')) {
@@ -2257,11 +2263,11 @@ object JsonCodecMaker {
2257
2263
else q " out.writeNonEscapedAsciiVal(x.name) "
2258
2264
}
2259
2265
} else if (isTuple(tpe)) withEncoderFor(methodKey, m) {
2260
- val writeFields = tpe. typeArgs.map {
2266
+ val writeFields = typeArgs(tpe) .map {
2261
2267
var i = 0
2262
- ta =>
2268
+ t =>
2263
2269
i += 1
2264
- genWriteVal(q " x. ${TermName (" _" + i)}" , ta.dealias :: types, isStringified, EmptyTree )
2270
+ genWriteVal(q " x. ${TermName (" _" + i)}" , t :: types, isStringified, EmptyTree )
2265
2271
}
2266
2272
q """ out.writeArrayStart()
2267
2273
.. $writeFields
0 commit comments