In continuation of this question, I have the following code but would like to map null
JSON values to Scala None. Current behavior I am getting is that there's no difference between including a key
with null
("key": null)
and not including it. I would like to map this to None
so I can set the DB entry to null
. Also, when a key
is not included in the JSON, map it to existing value.
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)
And then:
scala> println(update.map(_(existingPerson)))
Right(Person(mr updated,42,Some(tall and fat)))
But I would like to get:
Right(Person(mr updated,42,None))