My sample json is either with a country object Json sample 1
"@version": "1.0",
"country": {
"@country": "US",
"day": {
"@date": "2016-02-15",
"@value": "1"
}
}
or with country array: Json sample 2
"@version": "1.0",
"country": [{
"@country": "US",
"day": {
"@date": "2016-02-15",
"@value": "1"
}
}, {
"@country": "UK",
"day": {
"@date": "2016-02-15",
"@value": "5"
}]
}
To read the json
implicit val dayJsonReads: Reads[DayJson] = (
(JsPath \ "@date").read[DateTime](dateReads) and
((JsPath \ "@value").read[Int] orElse (JsPath \ "@value").read[String].map(_.toInt))
)(DayJson.apply _)
implicit val countryJsonReads: Reads[CountryJson] = (
(JsPath \ "@country").read[String] and
(JsPath \ "day").read[DayJson]
)(CountryJson.apply _)
implicit val newUserJsonReads: Reads[NewUserJson] = (
(JsPath \ "@version").read[String] and
(JsPath \ "country").readNullable[Seq[CountryJson]]
)(NewUserJsonParent.apply _)
The above code reads sample json 2 however fails for sample json 1. Is it possible to use readNullable to read either JS Value or JS Object or can we convert it from JS Value to JS Object. Thank you.