In Play 2.3, I have a case class with a single optional double member:
case class SomeClass(foo: Option[Double])
I need a JSON write converter that handles the member as nullable:
implicit val someClassWrite: Writes[SomeClass] = ???
The Play docs provide an example:
case class DisplayName(name:String)
implicit val displayNameWrite: Writes[DisplayName] = Writes {
(displayName: DisplayName) => JsString(displayName.name)
}
But sadly I can't figure out how to do this for 1) a single nullable and 2) a double. Any ideas? Thanks.
Update #1: The only solution I can come up with is this:
implicit val someClassWrite: Writes[SomeClass] = Writes {
(someClass: SomeClass) => someClass.foo match {
case Some(f) => JsNumber(BigDecimal(f))
case _ => JsNull
}
Update #2: Ignore my solution. Travis Brown's is the one.