Skip to content

Commit c51c46a

Browse files
committed
Add an example of derive keyword usage for a complex generic type
1 parent 6a7d7ef commit c51c46a

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1786,7 +1786,7 @@ object JsonCodecMaker {
17861786
tpe match
17871787
case ConstantType(c) => Literal(c).asExprOf[T]
17881788
case _ => cannotFindValueCodecError(tpe)
1789-
} else if (isEnumOrModuleValue(tpe)) Ref(tpe.termSymbol).asExprOf[T]
1789+
} else if (isEnumOrModuleValue(tpe)) Ref(tpe.termSymbol).asExprOf[T] // FIXME: add support of non top-level defiend enums
17901790
else if (isValueClass(tpe)) {
17911791
val tpe1 = valueClassValueType(tpe)
17921792
tpe1.asType match
@@ -2632,7 +2632,7 @@ object JsonCodecMaker {
26322632
if ($in.isNextToken('{')) {
26332633
$in.rollbackToken()
26342634
$in.skip()
2635-
${if (tpe =:= TypeRepr.of[None.type]) '{ None }.asExprOf[T] else Ref(tpe.termSymbol).asExprOf[T]}
2635+
${if (tpe =:= TypeRepr.of[None.type]) '{ None }.asExprOf[T] else Ref(tpe.termSymbol).asExprOf[T]} // FIXME: add support of non top-level defiend enums
26362636
} else $in.readNullOrTokenError($default, '{')
26372637
}
26382638
} else if (isSealedClass(tpe)) withDecoderFor(methodKey, default, in) { (in, default) =>

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

+13-1
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,17 @@ class JsonCodecMakerNewKeywordSpec extends VerifyingSpec {
8484
verifySerDeser(summon[JsonValueCodec[TestEnum]], TestEnum.Value2("VVV"), """{"hint":"Value2","str":"VVV"}""")
8585
}
8686
}
87+
"serialize and deserialize a complex generic types defined with `derives` keyword and a custom compile-time configurations" in {
88+
verifySerDeser(make[LinkedList[Int]](CodecMakerConfig.withAllowRecursiveTypes(true)),
89+
LinkedList.Node2[Int](2, LinkedList.Node[Int](1, LinkedList.End)),
90+
"""{"type":"Node2","value":2,"next":{"value":1,"next":{"type":"End"}}}""")
91+
}
8792
}
88-
}
93+
}
94+
95+
inline given CodecMakerConfig = CodecMakerConfig.withAllowRecursiveTypes(true)
96+
97+
enum LinkedList[+T] derives ConfiguredJsonValueCodec: // Borrowed from https://github.com/com-lihaoyi/upickle/pull/607
98+
case End
99+
case Node(value: T, next: LinkedList[T])
100+
case Node2(value: T, next: Node[T])

0 commit comments

Comments
 (0)