@TravisBrown helped me yesterday with an implicit function that returned a Writes[T],我工作得很好,但今天我尝试将其改编为另一个例子并在最后一行得到错误(“应用程序不接受参数”)。我做错了什么?
请忽略toString
方法,它们只是用于调试。
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
}
答案 0 :(得分:4)
我明白了。我需要改变这个:
(__ \ 'value.write[Int] and
到此:
(__ \ 'value.write[Float] and
抱歉打扰你!