我想在 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 _)

有谁知道如何做到这一点?

最佳答案

使用 Reads.pure 生成产生常量值的 Reads

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

10-07 23:42