Skip to content

Commit 1fabfee

Browse files
committed
Fix #89 by adding support of default values for Java enums in case of null value
1 parent 9a936b2 commit 1fabfee

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

macros/src/main/scala/com/github/plokhotnyuk/jsoniter_scala/macros/JsonCodecMaker.scala

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -586,10 +586,13 @@ object JsonCodecMaker {
586586
.getOrElse(in.enumValueError(len))
587587
} else in.readNullOrTokenError(default, '"')"""
588588
} else if (tpe <:< typeOf[java.lang.Enum[_]]) withDecoderFor(methodKey, default) {
589-
q"""val v = in.readString()
590-
try ${companion(tpe)}.valueOf(v) catch {
591-
case _: IllegalArgumentException => in.enumValueError(v)
592-
}"""
589+
q"""if (in.isNextToken('"')) {
590+
in.rollbackToken()
591+
val v = in.readString(null)
592+
try ${companion(tpe)}.valueOf(v) catch {
593+
case _: IllegalArgumentException => in.enumValueError(v)
594+
}
595+
} else in.readNullOrTokenError(default, '"')"""
593596
} else if (tpe.typeSymbol.isModuleClass) withDecoderFor(methodKey, default) {
594597
q"""if (in.isNextToken('{')) {
595598
in.rollbackToken()

macros/src/test/scala/com/github/plokhotnyuk/jsoniter_scala/macros/JsonCodecMakerSpec.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ class JsonCodecMakerSpec extends WordSpec with Matchers {
125125
val stringified = Stringified(1, 2, List(1), List(2))
126126
val codecOfStringified: JsonValueCodec[Stringified] = make[Stringified](CodecMakerConfig())
127127

128-
case class Defaults(s: String = "VVV", i: Int = 1, bi: BigInt = -1, oc: Option[Char] = Some('X'), l: List[Int] = List(0))
128+
case class Defaults(s: String = "VVV", i: Int = 1, bi: BigInt = -1, oc: Option[Char] = Some('X'),
129+
l: List[Int] = List(0), e: Level = Level.HIGH)
129130

130131
val defaults = Defaults()
131132
val codecOfDefaults: JsonValueCodec[Defaults] = make[Defaults](CodecMakerConfig())
@@ -410,7 +411,7 @@ class JsonCodecMakerSpec extends WordSpec with Matchers {
410411
Right(codecOfStandardTypes.decodeValue(in, codecOfStandardTypes.nullValue))
411412
case '"' =>
412413
in.rollbackToken()
413-
Left(in.readString())
414+
Left(in.readString(null))
414415
case _ =>
415416
in.decodeError("expected '{' or '\"'")
416417
}
@@ -814,7 +815,7 @@ class JsonCodecMakerSpec extends WordSpec with Matchers {
814815
}
815816
"deserialize default values in case of missing field or null/empty values" in {
816817
verifyDeser(codecOfDefaults, defaults, """{}""".getBytes)
817-
verifyDeser(codecOfDefaults, defaults, """{"s":null,"bi":null,"l":null,"oc":null}""".getBytes)
818+
verifyDeser(codecOfDefaults, defaults, """{"s":null,"bi":null,"l":null,"oc":null,"e":null}""".getBytes)
818819
verifyDeser(codecOfDefaults, defaults, """{"l":[]}""".getBytes)
819820
}
820821
"don't serialize and deserialize transient and non constructor defined fields of case classes" in {

0 commit comments

Comments
 (0)