Skip to content

Commit 7eb0fc2

Browse files
committed
Skip resetting presence bits for optional fields when checking of field duplication is disabled
1 parent 76fed0e commit 7eb0fc2

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1320,9 +1320,11 @@ object JsonCodecMaker {
13201320
i += 1
13211321
val n = paramVarNames(i >> 5)
13221322
val m = 1 << i
1323+
val nm = ~m
13231324
(fieldInfo.mappedName, {
13241325
if (cfg.checkFieldDuplication) q"if (($n & $m) != 0) $n ^= $m else in.duplicatedKeyError(l)"
1325-
else q"$n &= ~$m"
1326+
else if (required(fieldInfo.mappedName)) q"$n &= $nm"
1327+
else EmptyTree
13261328
})
13271329
}.toMap
13281330
val paramVars =

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1916,13 +1916,15 @@ object JsonCodecMaker {
19161916
val readVal = genReadVal(fTpe :: types, tmpVar.asExprOf[ft], fieldInfo.isStringified, false, in).asTerm
19171917
val n = Ref(paramVars(fieldInfo.nonTransientFieldIndex >> 5).symbol).asExprOf[Int]
19181918
val m = Expr(1 << fieldInfo.nonTransientFieldIndex)
1919+
val nm = Expr(~(1 << fieldInfo.nonTransientFieldIndex))
19191920
Block(List({
19201921
if (cfg.checkFieldDuplication) {
19211922
'{
19221923
if (($n & $m) != 0) ${Assign(n.asTerm, '{ $n ^ $m }.asTerm).asExprOf[Unit]}
19231924
else $in.duplicatedKeyError($l)
19241925
}.asTerm
1925-
} else Assign(n.asTerm, '{ $n & ~$m }.asTerm)
1926+
} else if (required(fieldInfo.mappedName)) Assign(n.asTerm, '{ $n & $nm }.asTerm)
1927+
else '{ }.asTerm
19261928
}), Assign(tmpVar, readVal)).asExprOf[Unit]
19271929
}
19281930
'{

0 commit comments

Comments
 (0)