title: Scala Play - How to format Generics for JSON conversion
body: <p>I am learning more and more about Scala and that nice playframework. But there are some things that bother me and that I can't get to work.</p>
  
  <p>I like using Generics for some kind of collections, for example. But I need those to be stored in our database, in JSON. There is this cool auto conversion thing, but it does not work for generics, in no way I have tried :-/</p>
  
  <p>Okay, to be concrete, code first:</p>
  
  <pre><code>case class InventorySlot(id: Long, item: Option[Item])
  
  object InventorySlot {
    implicit val fmt = Json.format[InventorySlot]
  }
  
  
  case class Inventory[T &lt;: Item](slots: Vector[InventorySlot]) {
    def length = slots.length
  
    def items: Vector[T] = slots.map(slot =&gt; slot.item).flatten.asInstanceOf[Vector[T]]
  
    def item(id: Long): Option[T] = {
      slots.find(_.id == id) match {
        case Some(slot: InventorySlot) =&gt;
          Some(slot.item.asInstanceOf[T])
        case None =&gt;
          Logger.warn(s"slot with id $id not found")
          None
      }
    }
  }
  
  object Inventory {
    implicit val fmt = Json.format[Inventory]
  }
  </code></pre>
  
  <p>Item is a basic abstract class of different items that can be put in that inventory. It doesn't matter. But sometimes I want to have an inventory, that just works for ItemType A, lets call it <code>AItem</code>.
  So I want to create my inventory with something like this:
  <code>val myInventory = Inventory[AItem]("content of vector here")</code> and when I call <code>myInventory.item(2)</code>, then I want to get the item in slot 2, and it should be an object of type <code>AItem</code>, not just <code>Item</code>. (That's the reason why I am using generics here)</p>
  
  <h1>So the problem</h1>
  
  <p>The implicit format for <code>Inventory</code> does not work, obviously.
  <code>Item</code> does, also with all special items, I can post the code for it below, and <code>InventorySlot</code> should work as well.</p>
  
  <p>The error when compiling is:</p>
  
  <pre><code>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]
                                    ^
  </code></pre>
  
  <p>I tried to write the read and write explicitly, like</p>
  
  <pre><code>implicit val fmt = (
    (__ \ "slots").format[Vector[InventorySlot]]
    )(Inventory.apply, unlift(Inventory.unapply))
  </code></pre>
  
  <p>wich is not even working in my IDE, and I can't find the problem.
  I am confused. I don't know where my error lies, or if I am doing something wrong, or if I just miss something.</p>
  
  <p>Any help will be appreciated.</p>
  
  <p>I am so helpless, I even have considered doing a class for all possible inventory types, like</p>
  
  <pre><code>case class AItemInventory(protected var slots: Vector[InventorySlot]) extends Inventory[AItem](slots)
  
  object AItemInventory {
    implicit val fmt = Json.format[AItemInventory]
  }
  </code></pre>
  
  <p>wich works. No problems, everything fine. So... I don't understand. Why is this working if it seems to be exactly the same, just hardcoded?</p>
  
  <h2>Appendix</h2>
  
  <p>The item formatter, wich works:</p>
  
  <pre><code>implicit val itemFormat = new Format[Item] {
    override def reads(json: JsValue): JsResult[Item] = {
      (json \ "itemType").as[ItemType] match {
        case ItemType.AITEM =&gt; fmtAItem.reads(json)
      }
    }
  
    override def writes(item: Item): JsValue = item match {
      case subItem: AItem =&gt; fmtAItem.writes(subItem)
      case _ =&gt; JsNumber(item.itemType.id)
    }
  }
  </code></pre>