-
-
Notifications
You must be signed in to change notification settings - Fork 161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
read json throws NullPointerException with Scala3 #371
Comments
Another example with Scala 3.1.1 and upickle 2.0.0; not sure if it's the same etiology or not. Stacktrace:
Code: import upickle.default._
case class TestClass(val map: Map[String, Boolean | Int])
object TestClass {
implicit val rw: upickle.default.ReadWriter[TestClass] =
upickle.default.macroRW
implicit val unionRw: upickle.default.ReadWriter[Boolean | Int] =
upickle.default
.readwriter[String]
.bimap[Boolean | Int](
x => {
println(s"DEBUG: x = ${x.toString}")
x.toString
},
a => {
println(s"DEBUG: a = ${a}")
a match {
case "true" => true
case "false" => false
case x => x.toInt
}
}
)
}
def main(): Unit = {
val myStr = """
{"map":{
"x":"1","y":"-2","z":"true"
}}
"""
// Bad
upickle.default.read[TestClass](myStr)
// OK
upickle.default.read[Map[String, Map[String, String]]](myStr)
val myObj = TestClass(Map("x" -> 1, "y" -> -2, "z" -> true))
// Bad
upickle.default.read[TestClass](
upickle.default.write(myObj)
)
// OK
upickle.default.read[Map[String, Map[String, String]]](
upickle.default.write(myObj)
)
} |
I've experienced the same issue and found a (even more Scala3 idiomatic) workaround: replace the I'm not sure if this is a compiler or a macro issue. In the code below it actually is sufficient to replace only one final case class Status(statusDetail: Detail)
enum Detail:
case NoDetail, SomeDetail
// given rwStatus : ReadWriter[Status] = macroRW
// given rwDetail: ReadWriter[Detail] =
// readwriter[Int].bimap[Detail](_.ordinal, Detail.fromOrdinal)
implicit val rwStatus : ReadWriter[Status] = macroRW
implicit val rwDetail: ReadWriter[Detail] = readwriter[Int].bimap[Detail](_.ordinal, Detail.fromOrdinal)
object TestUpickle:
def main(args : Array[String]) =
val someValue = Status(Detail.NoDetail)
println(write(someValue))
//Throws NPE when using implicits but works for given
println(read[Status](write(someValue)))
end TestUpickle
Versions:
Thrown Exception:
|
Reading json generated by write throws NullPointerException with Scala3, the same code with Scala2.13 runs without problems.
build.sbt
Code reproducing the problem
Fails with:
The text was updated successfully, but these errors were encountered: