I am currently using the Play framework json parser in order to parse a json string in my scala code.
I have the following class:
case class Address(address: String,
gps: GPS,
country: String) {}
object Address {
implicit val reads: Reads[Address] = (
(JsPath \ "address").read[String] and
(JsPath \ "gps").read[GPS] and
(JsPath \ "country").read[String]
) (Address.apply _)
implicit val writes: Writes[Address] = (
(JsPath \ "address").write[String] and
(JsPath \ "gps").write[GPS] and
(JsPath \ "country").write[String]
) (unlift(Address.unapply))
}
Which works fine with the following json:
{
"address": "123 Fake Street",
"country": "USA",
"gps": { ... }
}
The problem is that in some situations the json may instead have the gps field be a string which doesnt parse, i.e.
{
"address": "123 Fake Street",
"country": "USA",
"gps": "123abc"
}
Now I know that I cant have the gps member be both a string or a GPS object, but is there any way to have it be say an Option[GPS] and only have a value if the json contained a gps object?