JSON reads with hardcoded values ​​in a rendering framework (Scala)

I would like to use hard-coded values ​​in implicit JSON reads 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?

+3


source to share


2 answers


Use Reads.pure

for creations Reads

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

      

+4


source


You can do this using a custom istead function 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))
}

      

+2


source







All Articles