@@ -781,8 +781,9 @@ object JsonCodecMaker {
781
781
case ConstantType (_) => true
782
782
case _ => false
783
783
784
- def isEnumOrModuleValue (tpe : TypeRepr ): Boolean =
785
- (tpe.typeSymbol.flags.is(Flags .Module ) || tpe.termSymbol.flags.is(Flags .Enum ))
784
+ def isEnumValue (tpe : TypeRepr ): Boolean = tpe.termSymbol.flags.is(Flags .Enum )
785
+
786
+ def isEnumOrModuleValue (tpe : TypeRepr ): Boolean = isEnumValue(tpe) || tpe.typeSymbol.flags.is(Flags .Module )
786
787
787
788
def isOption (tpe : TypeRepr , types : List [TypeRepr ]): Boolean = tpe <:< TypeRepr .of[Option [_]] &&
788
789
(cfg.skipNestedOptionValues || ! types.headOption.exists(_ <:< TypeRepr .of[Option [_]]))
@@ -1130,7 +1131,7 @@ object JsonCodecMaker {
1130
1131
def adtLeafClasses (adtBaseTpe : TypeRepr ): Seq [TypeRepr ] = {
1131
1132
def collectRecursively (tpe : TypeRepr ): Seq [TypeRepr ] =
1132
1133
val leafTpes = adtChildren(tpe).flatMap { subTpe =>
1133
- if (isEnumOrModuleValue(subTpe) || subTpe =:= TypeRepr .of[ None . type ] ) Seq (subTpe)
1134
+ if (isEnumOrModuleValue(subTpe)) Seq (subTpe)
1134
1135
else if (isSealedClass(subTpe)) collectRecursively(subTpe)
1135
1136
else if (isValueClass(subTpe)) {
1136
1137
fail(" 'AnyVal' and one value classes with 'CodecMakerConfig.withInlineOneValueClasses(true)' are not " +
@@ -1531,15 +1532,19 @@ object JsonCodecMaker {
1531
1532
else ' { $in.unexpectedKeyError($l) }
1532
1533
1533
1534
def discriminatorValue (tpe : TypeRepr ): String =
1534
- val isEnum = tpe.termSymbol.flags.is( Flags . Enum )
1535
- var named = (if (isEnum ) tpe.termSymbol else tpe.typeSymbol).annotations.filter(_.tpe =:= TypeRepr .of[named])
1535
+ val named =
1536
+ (if (isEnumValue(tpe) ) tpe.termSymbol else tpe.typeSymbol).annotations.filter(_.tpe =:= TypeRepr .of[named])
1536
1537
if (named.nonEmpty) {
1537
1538
if (named.size > 1 ) fail(s " Duplicated ' ${TypeRepr .of[named].show}' defined for ' ${tpe.show}'. " )
1538
1539
namedValueOpt(named.headOption, tpe).get
1539
1540
} else cfg.adtLeafClassNameMapper({
1540
- if (tpe =:= TypeRepr .of[None .type ]) " scala.None"
1541
- else if (isEnum || tpe.typeSymbol.flags.is(Flags .Module )) tpe.termSymbol.fullName
1542
- else tpe.typeSymbol.fullName
1541
+ if (isEnumOrModuleValue(tpe)) {
1542
+ if (tpe.isSingleton) tpe.termSymbol.fullName
1543
+ else {
1544
+ val name = tpe.typeSymbol.fullName
1545
+ name.substring(0 , name.length - 1 )
1546
+ }
1547
+ } else tpe.typeSymbol.fullName
1543
1548
}).getOrElse(fail(s " Discriminator is not defined for ${tpe.show}" ))
1544
1549
1545
1550
def checkFieldNameCollisions (tpe : TypeRepr , names : Seq [String ]): Unit =
@@ -1788,7 +1793,7 @@ object JsonCodecMaker {
1788
1793
case ConstantType (c) => Literal (c).asExprOf[T ]
1789
1794
case _ => cannotFindValueCodecError(tpe)
1790
1795
} else if (isEnumOrModuleValue(tpe)) {
1791
- Ref (if (tpe.termSymbol.flags.is( Flags . Enum )) tpe.termSymbol else tpe.typeSymbol.companionModule).asExprOf[T ]
1796
+ Ref (if (isEnumValue(tpe )) tpe.termSymbol else tpe.typeSymbol.companionModule).asExprOf[T ]
1792
1797
} else if (isValueClass(tpe)) {
1793
1798
val tpe1 = valueClassValueType(tpe)
1794
1799
tpe1.asType match
@@ -1830,9 +1835,8 @@ object JsonCodecMaker {
1830
1835
$in.rollbackToMark()
1831
1836
$ {genReadLeafClass[st](subTpe)}
1832
1837
} else if (! cfg.circeLikeObjectEncoding && isEnumOrModuleValue(subTpe)) {
1833
- Ref (if (subTpe.termSymbol.flags.is(Flags .Enum )) subTpe.termSymbol else subTpe.typeSymbol.companionModule).asExprOf[T ]
1834
- } else if (! cfg.circeLikeObjectEncoding && subTpe =:= TypeRepr .of[None .type ]) ' { None }.asExprOf[st]
1835
- else genReadLeafClass[st](subTpe)
1838
+ Ref (if (isEnumValue(subTpe)) subTpe.termSymbol else subTpe.typeSymbol.companionModule).asExprOf[T ]
1839
+ } else genReadLeafClass[st](subTpe)
1836
1840
} else $acc
1837
1841
}.asExprOf[T ]
1838
1842
}
@@ -1902,7 +1906,7 @@ object JsonCodecMaker {
1902
1906
1903
1907
if (currentDiscriminator.isEmpty) {
1904
1908
val (leafModuleClasses, leafCaseClasses) = leafClasses.partition { x =>
1905
- ! cfg.circeLikeObjectEncoding && ( isEnumOrModuleValue(x) || x =:= TypeRepr .of[ None . type ] )
1909
+ ! cfg.circeLikeObjectEncoding && isEnumOrModuleValue(x)
1906
1910
}
1907
1911
if (leafModuleClasses.nonEmpty && leafCaseClasses.nonEmpty) {
1908
1912
' {
@@ -2630,8 +2634,8 @@ object JsonCodecMaker {
2630
2634
if ($in.isNextToken('[' )) $ {readCreateBlock.asExprOf[T ]}
2631
2635
else $in.readNullOrTokenError($default, '[' )
2632
2636
}
2633
- } else if (isEnumOrModuleValue(tpe) || tpe =:= TypeRepr .of[ None . type ] ) withDecoderFor(methodKey, default, in) { (in, default) =>
2634
- val x = Ref (if (tpe.termSymbol.flags.is( Flags . Enum )) tpe.termSymbol else tpe.typeSymbol.companionModule).asExprOf[T ]
2637
+ } else if (isEnumOrModuleValue(tpe)) withDecoderFor(methodKey, default, in) { (in, default) =>
2638
+ val x = Ref (if (isEnumValue(tpe )) tpe.termSymbol else tpe.typeSymbol.companionModule).asExprOf[T ]
2635
2639
' {
2636
2640
if ($in.isNextToken('{' )) {
2637
2641
$in.rollbackToken()
@@ -3067,8 +3071,7 @@ object JsonCodecMaker {
3067
3071
}
3068
3072
if (writeFields.isEmpty) fail(s " Expected that ${tpe.show} should be an applied type " )
3069
3073
Block (' { $out.writeArrayStart() }.asTerm :: writeFields, ' { $out.writeArrayEnd() }.asTerm).asExprOf[Unit ]
3070
- } else if ((isEnumOrModuleValue(tpe) || tpe =:= TypeRepr .of[None .type ]) &&
3071
- ! (cfg.alwaysEmitDiscriminator && hasSealedParent(tpe))) withEncoderFor(methodKey, m, out) { (out, x) =>
3074
+ } else if (isEnumOrModuleValue(tpe) && ! (cfg.alwaysEmitDiscriminator && hasSealedParent(tpe))) withEncoderFor(methodKey, m, out) { (out, x) =>
3072
3075
' {
3073
3076
$out.writeObjectStart()
3074
3077
$ {optWriteDiscriminator.fold(' {})(_.write(out))}
@@ -3084,7 +3087,7 @@ object JsonCodecMaker {
3084
3087
val leafClasses = adtLeafClasses(tpe)
3085
3088
val writeSubclasses = cfg.discriminatorFieldName.fold {
3086
3089
leafClasses.map { subTpe =>
3087
- if (! cfg.circeLikeObjectEncoding && ( isEnumOrModuleValue(subTpe) || subTpe =:= TypeRepr .of[ None . type ] )) {
3090
+ if (! cfg.circeLikeObjectEncoding && isEnumOrModuleValue(subTpe)) {
3088
3091
CaseDef (Typed (x.asTerm, Inferred (subTpe)), None ,
3089
3092
genWriteConstantVal(discriminatorValue(subTpe), out).asTerm)
3090
3093
} else {
0 commit comments