【发布时间】:2023-03-03 00:31:01
【问题描述】:
继续question,我有以下代码,但想将null JSON 值映射到 Scala None。我得到的当前行为是包含key 和null ("key": null) 和不包含它之间没有区别。我想将其映射到None,这样我就可以将数据库条目设置为null。此外,当 JSON 中不包含 key 时,将其映射到现有值。
import io.circe.jawn.decode, io.circe.generic.auto._
case class Person(name: String, age: Int, description: Option[String])
val existingPerson = Person("mr complete", 42, Some("tall and fat"))
val incompletePersonJson = """{"description":null}"""
val update = decode[Person => Person](incompletePersonJson)
然后:
scala> println(update.map(_(existingPerson)))
Right(Person(mr updated,42,Some(tall and fat)))
但我想得到:
Right(Person(mr updated,42,None))
【问题讨论】:
标签: json scala dictionary null circe