Null values in JSON produce an NPE if we attempt to unpickle into a field with an AnyVal type. The following two tests are the same except Foo extends AnyVal in the first one:
object AnyValTest {
final case class Foo(a: String) extends AnyVal
implicit val fooReader = upickle.default.Reader[Foo] {
case upickle.Js.Str(s) => Foo(s)
}
final case class Bar(foo: Foo)
def fails = upickle.default.read[Bar]("""
{ "foo": null }
""")
def works = upickle.default.read[Bar]("""
{ "foo": "x" }
""")
}
object AnyRefTest {
final case class Foo(a: String)
implicit val fooReader = upickle.default.Reader[Foo] {
case upickle.Js.Str(s) => Foo(s)
}
final case class Bar(foo: Foo)
def works = upickle.default.read[Bar]("""
{ "foo": null }
""")
}
AnyValTest.works and AnyRefTest.works produce the expected results, but AnyValTest.fails gives:
It seems to be the case that AnyVal references can't be null (weirdly it does work if you coerce to make the compiler happy: Bar(null.asInstanceOf[Foo])). So it's likely that this should not work, but it would be great if upickle could detect this case and give a better error. It took me a long time to figure out that the attempted null AnyVal was the issue.
Null values in JSON produce an NPE if we attempt to unpickle into a field with an
AnyVal
type. The following two tests are the same exceptFoo
extendsAnyVal
in the first one:AnyValTest.works
andAnyRefTest.works
produce the expected results, butAnyValTest.fails
gives:It seems to be the case that
AnyVal
references can't be null (weirdly it does work if you coerce to make the compiler happy:Bar(null.asInstanceOf[Foo])
). So it's likely that this should not work, but it would be great if upickle could detect this case and give a better error. It took me a long time to figure out that the attempted nullAnyVal
was the issue.ID: 202 Original Author: quelgar