Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON reads with hardcoded values in Play framework (Scala)

I'd like to use hardcoded values in JSON implicit reads, something like:

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 _)

Does anyone know how to do this?

like image 940
bjfletcher Avatar asked Feb 11 '23 05:02

bjfletcher


1 Answers

Use Reads.pure to produce Reads that yield a constant value.

implicit val locationReads: Reads[Location] = (
  Reads.pure("I am a hardcoded value") and
  (JsPath \ "lat").read[Double] and
  (JsPath \ "long").read[Double]
)(Location.apply _)
like image 190
Michael Zajac Avatar answered Feb 16 '23 03:02

Michael Zajac