title: Implementing play! json read/write for type parametric class
body: <p>i'm dealing with the following:</p>
  
  <pre><code>&gt;&gt;&gt; A.scala 
  abstract class A[T &lt;: A[T]](field:String){
     def ~&gt;(a : T):Boolean
  }
  &lt;&lt;&lt;&lt;
  
  &gt;&gt;&gt; B.scala
  case class B(field:String,field1:String) extends A[B](field){
    override def ~&gt;(other: B): Boolean = 
     field == other.field &amp;&amp; field1 == other.field1
  }
  object B{
  
   implicit object BFormat extends Format[B] {
  
    override def reads(json:JsValue):JsResult[B] = JsSuccess(new B(
      (json \ "pathTo" \ "field").as[String],
      (json \ "pathTo" \ "field1).as[String]
    ))
    override def writes(b:B):JsValue = Json.obj(
      "field" -&gt; b.field,
      "field1" -&gt; b.field1
    )
  
   }
  
   def simpleB = new B("foo","bar")
  }
  &lt;&lt;&lt;&lt;
  </code></pre>
  
  <p>The parameter <code>T</code> in the superclass <code>A</code> is used (indirectly) in the method <code>~&gt;</code> that performs a match between <strong>this</strong> and the other object which must be of the same type. It works but
  now i want to write read/write format combinators of Play Framework! 2.2.2 json library for the class <code>A</code> but i'm stuck because i don't know how to implement that, using a companion object i couldn't annotate type parameters (in the companion object) and even if i extend <code>Format[A[T]]</code> i don't know how to implement </p>
  
  <pre><code>implicit def reads(json: JsValue): JsResult[A[T]]
  implicit def writes(o: A[T]): JsValue
  </code></pre>
  
  <p>I already know the subclasses of <code>A</code>, they are two and already implements read/writes for json but i am not able to do so for the superclass <code>A</code> which has a type parameter (of which i know i can use json read/writes), any clue?</p>
  
  <p>Example and error:</p>
  
  <pre><code>def getMyA[T &lt;: A[T]](json:JsValue):Option[T] =
   (json \ "mypath").asOpt[T]
  
   diverging implicit expansion for type play.api.libs.json.Reads[T] starting with method GenericFormat in trait DefaultFormat
  </code></pre>
  
  <p>thanks</p>