Scala Play - 如何格式化泛型以进行 JSON 转换

Scala Play - How to format Generics for JSON conversion

我对 Scala 和那个不错的游戏框架的了解越来越多。但是有些事情困扰着我,我不能去上班。

例如,我喜欢将泛型用于某种集合。但我需要将它们存储在我们的数据库中,在 JSON 中。有这个很酷的自动转换功能,但它不适用于泛型,我从来没有尝试过:-/

好吧,具体点,先上代码:

case class InventorySlot(id: Long, item: Option[Item])

object InventorySlot {
  implicit val fmt = Json.format[InventorySlot]
}


case class Inventory[T <: Item](slots: Vector[InventorySlot]) {
  def length = slots.length

  def items: Vector[T] = slots.map(slot => slot.item).flatten.asInstanceOf[Vector[T]]

  def item(id: Long): Option[T] = {
    slots.find(_.id == id) match {
      case Some(slot: InventorySlot) =>
        Some(slot.item.asInstanceOf[T])
      case None =>
        Logger.warn(s"slot with id $id not found")
        None
    }
  }
}

object Inventory {
  implicit val fmt = Json.format[Inventory]
}

项目是可以放入该库存的不同项目的基本摘要class。没关系。但有时我想要一个仅适用于 ItemType A 的库存,我们称之为 AItem。 所以我想用这样的东西创建我的库存: val myInventory = Inventory[AItem]("content of vector here") 当我调用 myInventory.item(2) 时,我想获取插槽 2 中的项目,它应该是 AItem 类型的对象,而不仅仅是 Item。 (这就是我在这里使用泛型的原因)

所以问题

Inventory 的隐式格式显然不起作用。 Item 可以,对于所有特殊物品,我可以 post 下面的代码,InventorySlot 应该也可以。

编译时的错误是:

Error:(34, 34) Play 2 Compiler: 
 C:\depot\mars\mainline\server\app\models\Test.scala:34: class Inventory takes type parameters
   implicit val fmt = Json.format[Inventory]
                                  ^

我试过显式写读写,比如

implicit val fmt = (
  (__ \ "slots").format[Vector[InventorySlot]]
  )(Inventory.apply, unlift(Inventory.unapply))

wich 在我的 IDE 中甚至不工作,我找不到问题所在。 我很迷惑。我不知道我的错误在哪里,或者我做错了什么,或者我只是错过了什么。

任何帮助将不胜感激。

我很无奈,我什至考虑过对所有可能的库存类型做一个class,比如

case class AItemInventory(protected var slots: Vector[InventorySlot]) extends Inventory[AItem](slots)

object AItemInventory {
  implicit val fmt = Json.format[AItemInventory]
}

很管用。没问题,一切都很好。所以……我不明白。如果它看起来完全一样,只是硬编码,为什么它可以工作?

附录

项目格式化程序,有效:

implicit val itemFormat = new Format[Item] {
  override def reads(json: JsValue): JsResult[Item] = {
    (json \ "itemType").as[ItemType] match {
      case ItemType.AITEM => fmtAItem.reads(json)
    }
  }

  override def writes(item: Item): JsValue = item match {
    case subItem: AItem => fmtAItem.writes(subItem)
    case _ => JsNumber(item.itemType.id)
  }
}
object Inventory {
  implicit def fmt[T <: Item](implicit fmt: Format[T]): Format[Inventory[T]] = new Format[Inventory[T]] {
    def reads(json: JsValue): Inventory[T] = new Inventory[T] (
      (json \ "blah").as[String]
    )
    def writes(i: Inventory[T]) = JsObject(Seq(
      "blah" -> JsString(i.blah)
    ))
  }
}

来源:documentation 解释了如何进行读取和写入操作,我在这里所做的是将这两种格式结合起来。