I have a case class
case class Test(a: String, b: Option[Double])
object TestSerializer extends CustomSerializer[Test] (format => ({
case jv: JValue =>
val a = (jv \ "a").extract[String]
val b = (jv \ "b").extractOpt
Test(a, b)
},{
case tst: Test =>
tst.b match {
case Some(x) => ("a" -> "test") ~ ("b" -> x)
case None => ("a" -> "test") ~ ("b" -> "NA")
}
}))
When b is available, the result I get is: {a: "test", b: 1.0}
When b = None, the result I get is: {a: "test"}
The second result throws an exception in the first partial function since it cannot find b. How can I ensure my code does not fail and instead treat the missing b value of json as None?
I am using json4s 3.2.10 and not 3.2.11 so I cannot use the preserveEmpty fields option.