0

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.

pheeleeppoo
  • 1,491
  • 6
  • 25
  • 29
rreiki
  • 73
  • 3
  • 1
    Could you paste your exception in full – goralph May 17 '15 at 18:52
  • I cannot reproduce an error. Maybe you can post how you use the serializer? Tested the following with json4s native 2.11 v 3.2.10: http://pastebin.com/fX8uW7Wq – Kulu Limpa May 17 '15 at 21:00
  • I checked the versions. 3.2.10 hides null values...they introduced a new option in 3.2.11 to preserve empty values. – rreiki May 19 '15 at 19:14

0 Answers0