@TravisBrown helped me yesterday with an implicit function that returned a Writes[T], which I got working fine, but today I tried to adapt it for another example and got the error on the last line ("Application does not take parameters"). What am I doing wrong?
Please ignore the toString
methods, they're just there for debugging.
import play.api.libs.json._
import play.api.libs.functional.syntax._
sealed trait PressureUnit
case object HectoPascalsUnit extends PressureUnit { override def toString(): String = "(HectoPascalsUnit)" }
case object InchesMercuryUnit extends PressureUnit { override def toString(): String = "(InchesMercuryUnit)" }
case class Pressure(value: Float, unit: PressureUnit)
class SeaLevelPressure(override val value: Float, override val unit: PressureUnit) extends Pressure(value, unit) {
override def toString(): String = "(SeaLevelPressure value:"+value+" unit:"+unit+")"
}
class StationPressure(override val value: Float, override val unit: PressureUnit) extends Pressure(value, unit) {
override def toString(): String = "(StationPressure value:"+value+" unit:"+unit+")"
}
object Pressure {
implicit val pressureWrites: Writes[Pressure] = (
(__ \ 'value).write[Int] and
(__ \ 'unit).write[String].contramap[PressureUnit] {
case HectoPascalsUnit => "hPa"
case InchesMercuryUnit => "INS"
}
)(unlift(Pressure.unapply)) // Error: Application does not take parameters
}
I figured it out. I needed to change this:
(__ \ 'value.write[Int] and
to this:
(__ \ 'value.write[Float] and
Sorry to bother you!