为什么我得到“应用程序不带参数”,我该如何解决这个问题?

时间:2015-02-27 16:59:45

标签: json scala playframework playframework-json

@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
}

1 个答案:

答案 0 :(得分:4)

我明白了。我需要改变这个:

(__ \ 'value.write[Int] and

到此:

(__ \ 'value.write[Float] and

抱歉打扰你!