JSON使用Play框架(Scala)中的硬编码值读取

时间:2015-05-04 12:52:12

标签: json scala playframework

我想在JSON隐式读取中使用硬编码值,例如:

implicit val locationReads: Reads[Location] = (
  "I am a hardcoded value" and // something like that
  (JsPath \ "lat").read[Double] and
  (JsPath \ "long").read[Double]
)(Location.apply _)

有谁知道怎么做?

1 个答案:

答案 0 :(得分:3)

您可以使用自定义函数而不是Location.apply

来实现
case class Location(s: String, lat: Double, lon: Double)
object Location {
  implicit val locationReads: Reads[Location] = (
      (JsPath \ "lat").read[Double] and
      (JsPath \ "long").read[Double]
    )((lat, lon) => Location("I am a hardcoded value", lat, lon))
}