@@ -993,7 +993,7 @@ object JsonCodecMaker {
993
993
tpe match {
994
994
case AppliedType (_, List (nTpe, tTpe)) =>
995
995
// Borrowed from an amazing work of Aleksander Rainko: https://github.com/arainko/ducktape/blob/8d779f0303c23fd45815d3574467ffc321a8db2b/ducktape/src/main/scala/io/github/arainko/ducktape/internal/Structure.scala#L188-L199
996
- val names = tupleTypeArgs(nTpe.dealias.asType).map { case ConstantType (StringConstant (n )) => n }
996
+ val names = tupleTypeArgs(nTpe.dealias.asType).map { case ConstantType (StringConstant (name )) => name }
997
997
val typeArgs = tupleTypeArgs(tTpe.dealias.asType)
998
998
val size = typeArgs.size
999
999
val tupleTpe =
@@ -1020,7 +1020,7 @@ object JsonCodecMaker {
1020
1020
val constructor =
1021
1021
if (tpeTypeArgs.isEmpty) constructorNoTypes
1022
1022
else TypeApply (constructorNoTypes, tpeTypeArgs.map(Inferred (_)))
1023
- argss.tail.foldLeft(Apply (constructor, argss.head))((acc, args) => Apply (acc, args ))
1023
+ argss.tail.foldLeft(Apply (constructor, argss.head))(Apply (_, _ ))
1024
1024
}
1025
1025
1026
1026
val classInfos = new mutable.LinkedHashMap [TypeRepr , ClassInfo ]
@@ -2733,6 +2733,25 @@ object JsonCodecMaker {
2733
2733
optDiscriminator : Option [WriteDiscriminator ],
2734
2734
out : Expr [JsonWriter ])(using Quotes ): Expr [Unit ] =
2735
2735
val tpe = types.head
2736
+ val (valDefs, valRef) =
2737
+ typeInfo match
2738
+ case namedTupleInfo : NamedTupleInfo =>
2739
+ val valDef = ValDef (
2740
+ Symbol .newVal(Symbol .spliceOwner, " t" , namedTupleInfo.tupleTpe, Flags .EmptyFlags , Symbol .noSymbol),
2741
+ Some (
2742
+ Apply (
2743
+ Select
2744
+ .unique(Ref (Symbol .requiredModule(" scala.NamedTuple" )), " toTuple" )
2745
+ .appliedToTypeTrees(tpe.typeArgs.map { typeArg =>
2746
+ typeArg.asType match
2747
+ case ' [t] => TypeTree .of[t]
2748
+ }),
2749
+ List (x.asTerm)
2750
+ )
2751
+ )
2752
+ )
2753
+ (List (valDef), Ref (valDef.symbol))
2754
+ case _ => (Nil , x.asTerm)
2736
2755
val writeFields = typeInfo.fields.map { fieldInfo =>
2737
2756
val fDefault =
2738
2757
if (cfg.transientDefault) fieldInfo.defaultValue
@@ -2742,13 +2761,13 @@ object JsonCodecMaker {
2742
2761
case ' [ft] =>
2743
2762
val getter = typeInfo match
2744
2763
case namedTupleInfo : NamedTupleInfo =>
2745
- val idx = fieldInfo.nonTransientFieldIndex
2746
- if (namedTupleInfo.isGeneric) {
2747
- ' { $x. asInstanceOf [ Tuple ].productElement( $ { Expr ( idx)}). asInstanceOf [ft] }.asTerm
2748
- } else namedTupleInfo.tupleType match {
2749
- case ' [tt] => Select .unique( ' { $x. asInstanceOf [tt] }.asTerm, s " _ ${ idx + 1 } " )
2750
- }
2751
- case _ => Select (x.asTerm , fieldInfo.getterOrField)
2764
+ namedTupleInfo.tupleType match
2765
+ case ' [tt] =>
2766
+ val idx = fieldInfo.nonTransientFieldIndex
2767
+ if ( namedTupleInfo.isGeneric) {
2768
+ ' { $ {valRef.asExprOf [tt & Tuple ]}.productElement( $ { Expr ( idx)}). asInstanceOf [ft] }.asTerm
2769
+ } else Select .unique(valRef, s " _ ${idx + 1 } " )
2770
+ case _ => Select (valRef , fieldInfo.getterOrField)
2752
2771
fDefault match {
2753
2772
case Some (d) =>
2754
2773
if (cfg.transientEmpty && fTpe <:< TypeRepr .of[Iterable [_]]) ' {
@@ -2873,8 +2892,10 @@ object JsonCodecMaker {
2873
2892
}
2874
2893
}
2875
2894
val allWriteFields = optDiscriminator.fold(writeFields)(_.write(out) :: writeFields)
2876
- Block (' { $out.writeObjectStart() }.asTerm :: allWriteFields.map(_.asTerm.changeOwner(Symbol .spliceOwner)),
2877
- ' { $out.writeObjectEnd() }.asTerm).asExprOf[Unit ]
2895
+ Block (valDefs,
2896
+ Block (' { $out.writeObjectStart() }.asTerm :: allWriteFields.map(_.asTerm.changeOwner(Symbol .spliceOwner)),
2897
+ ' { $out.writeObjectEnd() }.asTerm)
2898
+ ).asExprOf[Unit ]
2878
2899
2879
2900
def getWriteConstType (tpe : TypeRepr , isStringified : Boolean , out : Expr [JsonWriter ])(using Quotes ): Expr [Unit ] =
2880
2901
tpe match
0 commit comments