Say I have a case class like:
case class KVP(customKey: Option[String], value: String)
I can create a JSON writes method like:
implicit val kvpWrites: Writes[KVP] = (
( __ \ "customKey").write[Option[String]] and
( __ \ "value").write[String]
)(unlift(KVP.unapply))
So the JSON will look something like for KVP("location", "NY")
{
"customKey": "location",
"value": "NY"
}
But what I want is this, if customKey has a value I want:
{
"location": "NY"
}
if customKey is a None, I want:
{
"value": "NY"
}
How can I achieve this?