2

我正在为单位可能变化的速度生成JSON 我有一个 SpeedUnit 特征和扩展它的类(Knots、MetersPerSecond、MilesPerHour)。JSON Play 文档说“要将您自己的模型转换为 JsValues,您必须定义隐式写入转换器并在范围内提供它们。” 我在大多数地方都能做到这一点,但当我有一个扩展特性的类时就不行了。我究竟做错了什么?或者是否有我可以或应该使用的 Enum 变体?

// Type mismatch: found (String, SpeedUnit), required (String, Json.JsValueWrapper)
// at 4th line from bottom:  "speedunit" -> unit

import play.api.libs.json._

trait SpeedUnit {
  // I added this to SpeedUnit thinking it might help, but it didn't.
  implicit val speedUnitWrites = new Writes[SpeedUnit] {
    def writes(x: SpeedUnit) = Json.toJson("UnspecifiedSpeedUnit")
  }
}

class Knots extends SpeedUnit {
  implicit val knotsWrites = new Writes[Knots] {
    def writes(x: Knots) = Json.toJson("KT")
  }
}
class MetersPerSecond extends SpeedUnit {
  implicit val metersPerSecondWrites = new Writes[MetersPerSecond] {
    def writes(x: MetersPerSecond) = Json.toJson("MPS")
  }
}
class MilesPerHour extends SpeedUnit {
  implicit val milesPerHourWrites = new Writes[MilesPerHour] {
    def writes(x: MilesPerHour) = Json.toJson("MPH")
  }
}

// ...

class Speed(val value: Int, val unit: SpeedUnit) {
  implicit val speedWrites = new Writes[Speed] {
    def writes(x: Speed) = Json.obj(
      "value" -> value,
      "speedUnit" -> unit  // THIS LINE DOES NOT TYPE-CHECK
    )
  }
}
4

1 回答 1

3

Writes是类型类的一个示例,这意味着您需要一个Writes[A]给定的 a 实例A,而不是每个A实例。如果您来自 Java 背景,请考虑Comparator而不是Comparable.

import play.api.libs.json._

sealed trait SpeedUnit
case object Knots extends SpeedUnit
case object MetersPerSecond extends SpeedUnit
case object MilesPerHour extends SpeedUnit

object SpeedUnit {
  implicit val speedUnitWrites: Writes[SpeedUnit] = new Writes[SpeedUnit] {
    def writes(x: SpeedUnit) = Json.toJson(
      x match {
        case Knots => "KTS"
        case MetersPerSecond => "MPS"
        case MilesPerHour => "MPH"
      }
    )
  }
}

case class Speed(value: Int, unit: SpeedUnit)

object Speed {
  implicit val speedWrites: Writes[Speed] = new Writes[Speed] {
    def writes(x: Speed) = Json.obj(
      "value" -> x.value,
      "speedUnit" -> x.unit
    )
  }
}

接着:

scala> Json.toJson(Speed(10, MilesPerHour))
res0: play.api.libs.json.JsValue = {"value":10,"speedUnit":"MPH"}

我已将Writes实例放在这两种类型的伴随对象中,但它们可以放在其他地方(例如,如果您不想在模型中混淆序列化问题)。

您还可以使用 Play JSON 的功能 API 简化(或至少简洁化)这一点:

sealed trait SpeedUnit
case object Knots extends SpeedUnit
case object MetersPerSecond extends SpeedUnit
case object MilesPerHour extends SpeedUnit

case class Speed(value: Int, unit: SpeedUnit)

import play.api.libs.json._
import play.api.libs.functional.syntax._

implicit val speedWrites: Writes[Speed] = (
  (__ \ 'value).write[Int] and
  (__ \ 'speedUnit).write[String].contramap[SpeedUnit] {
    case Knots => "KTS"
    case MetersPerSecond => "MPS"
    case MilesPerHour => "MPH"
  }
)(unlift(Speed.unapply))

您采用哪种方法(功能性或显式)很大程度上取决于口味。

于 2015-02-26T18:14:40.873 回答