I have the following scala code which works to parse my JSON into my case class UserLocation
.
Here usLocationDuration
or a nonUSLocationDuration
are optional; but what I want is a way to enforce that that the JSON requires a usLocationDuration
or a nonUSLocationDuration
, not both or not neither?
import play.api.libs.functional.syntax._
import play.api.libs.json._
final case class UserLocation(username: String, usLocationDuration: Option[Int], nonUSLocationDuration: Option[Int])
object UserLocation {
implicit val userReads: Reads[UserLocation] = (
(JsPath \ "username").read[String] and
(JsPath \ "usLocationDuration").readNullable[Int] and
(JsPath \ "nonUSLocationDuration").readNullable[Int]
) (UserLocation.apply _)
def main(args: Array[String]): Unit = {
val json = s"""{"username": "rs", "usLocationDuration": 10}"""
val user = Json.parse(json).as[UserLocation]
println(user)
}
}
final case class UserLocation(username: String, usLocationDuration: Option[Int], nonUSLocationDuration: Option[Int]) { require(usLocationDuration.nonEmpty != nonUSLocationDuration.nonEmpty ) }