播放2.4 参数化代数数据类型JSON验证

Play 2.4 parameterized algebraic data types JSON validation

是否可以在 Scala / Play 2.4 中做类似的事情:

sealed trait Location { val `type`: String }
case class CityLocation(zip_code: String, override val `type`: String = "city") extends Location
case class AddressLocation(
  society: Option[String],
  first_name: String,
  last_name: String,
  ...
  override val `type`: String = "address"
) extends Location

sealed trait Point[T <: Location] { val `type`: String; val location: T }
case class ShippingPoint[T](override val location: T, override val `type`: String = "shipping") extends Point[T]
case class PickupPoint[T](override val location: T, override val `type`: String = "pickup") extends Point[T]

object CityLocation {
  implicit val format = Json.format[CityLocation]
}
object AddressLocation {
  implicit val format = Json.format[AddressLocation]
}
object ShippingPoint {
  implicit def write[T] = Json.writes[ShippingPoint[T]]
  implicit def read[T]: Reads[ShippingPoint[T]] = (
    (__ \ "location").read[T] and
      (__ \ "type").read[String](exactWordRead("shipping", "error.route.shipping.type"))
  )(ShippingPoint[T].apply _)
}
object PickupPoint {
  implicit def write[T] = Json.writes[ShippingPoint[T]]
  implicit def read[T]: Reads[PickupPoint[T]] = (
    (__ \ "location").read[T] and
      (__ \ "type").read[String](exactWordRead("pickup", "error.route.pickup.type"))
  )(PickupPoint[T].apply _)
}

??

暂时还不能编译。

exactWordRead方法定义如下:

def exactWordRead(word: String, errorMessage: String): Reads[String] = Reads.constraints.pattern(s"^$word${"$"}".r, errorMessage)

编辑:

看来是因为这个回答让我又向前迈进了一步 :

sealed trait Location { val `type`: String }
case class CityLocation(zip_code: String, override val `type`: String = "city") extends Location
case class AddressLocation(
  society: Option[String],
  first_name: String,
  last_name: String,
  ...
  override val `type`: String = "address"
) extends Location

sealed trait Point[T <: Location] { val location: T; val `type`: String }
case class ShippingPoint[T](override val location: T, override val `type`: String = "shipping") extends Point[T]
case class PickupPoint[T](override val location: T, override val `type`: String = "pickup") extends Point[T]

object CityLocation {
  implicit val format = Json.format[CityLocation]
}
object AddressLocation {
  implicit val format = Json.format[AddressLocation]
}
object ShippingPoint {
  implicit def format[T: Format]: Format[ShippingPoint[T]] = (
    (__ \ "location").format[T] and
      (__ \ "type").format[String](exactWordRead("shipping", "error.route.shipping.type"))
  )(ShippingPoint.apply, unlift(ShippingPoint.unapply))
}
object PickupPoint {
  implicit def format[T: Format]: Format[PickupPoint[T]] = (
    (__ \ "location").format[T] and
      (__ \ "type").format[String](exactWordRead("pickup", "error.route.pickup.type"))
  )(PickupPoint.apply, unlift(PickupPoint.unapply))
}

但是,我还没有编译。编译错误是:

[error] type arguments [T] do not conform to trait Point's type parameter bounds [T <: services.coliswebApi.data.Location]
[error] case class ShippingPoint[T](override val location: T, override val `type`: String = "shipping") extends Point[T]
[error]                                                                                                         ^
[error] type arguments [T] do not conform to trait Point's type parameter bounds [T <: services.coliswebApi.data.Location]
[error] case class PickupPoint[T](override val location: T, override val `type`: String = "pickup") extends Point[T]

正如编译器所说,一方面你有,

sealed trait Point[T <: Location]

... 而在另一边,

case class ShippingPoint[T](???) extends Point[T]
case class PickupPoint[T](???) extends Point[T]

但是 ShippingPointPickupPoint 的声明中没有任何内容可以证明类型参数与扩展 Point.[=17 所需的约束 <: Location 兼容=]

所以你需要解决这个问题。

case class ShippingPoint[T <: Location](???) extends Point[T]
case class PickupPoint[T <: Location](???) extends Point[T]

我认为您还需要像这样以您的格式绑定类型

implicit def format[T <: Location: Format]: Format[ShippingPoint[T]] = (
        (__ \ "location").format[T] and
        (__ \ "type").format[String](exactWordRead("shipping",   "error.route.shipping.type"))
    )(ShippingPoint.apply, unlift(ShippingPoint.unapply))