title: How to parse a trait and objects to JSON in Scala Play Framework
body: <p>Currently I am working a web application using the Play Framework and now I am working on a JSON api. Unfortunately I have problems with parsing my objects to JSON with the built in JSON library. We have the following trait, which defines the type of Shipment and which parser to use. And a case class which has a ShipmentType so we know which parser to user for each type. And there is a method which returns all stored shipments as a list.</p>
  
  <pre><code>trait ShipmentType {
  
    def parser(list: List[String]): ShipmentTypeParser
  
  }
  
  object ShipmentTypeA extends ShipmentType {
  
    def parser(list: List[String]) = new ShipmentTypeAParser(list)
  
  }
  
  object ShipmentTypeB extends ShipmentType {
  
    def parser(list: List[String]) = new ShipmentTypeBParser(list)
  
  }
  
  object ShipmentTypeC extends ShipmentType {
  
    def parser(list: List[String]) = new ShipmentTypeCParser(list)
  
  }
  
  case class Shipment(id: Long, name: String, date: Date, shipmentType: Type)
  </code></pre>
  
  <p>To write this JSON I use the following implicit val:</p>
  
  <pre><code>import play.api.libs.json._
  import play.api.libs.functional.syntax._
  
  def findAll = Action {
      Ok(Json.toJson(Shipments.all))
  }
  implicit val shipmentWrites: Writes[Shipment] = (
      (JsPath \ "id").write[Option[Long]] and
      (JsPath \ "name").write[String] and
      (JsPath \ "date").write[Date] and
      (JsPath \ "shipmentType").write[ShipmentType]
  )(unlift(Shipment.unapply))
  </code></pre>
  
  <p>Next we need an extra one for the ShipmentType:</p>
  
  <pre><code>implicit val shipmentTypeWriter: Writes[ShipmentType] = ()
  </code></pre>
  
  <p>But there is where I get stuck, I cannot seem to find a way how to define the writer for the ShipmentType.</p>
  
  <p>I also tried defining them as follows according to another page of the Play Framework Documentation:</p>
  
  <pre><code>implicit val shipmentWrites: Writes[Shipment] = Json.writes[Shipment]
  implicit val shipmentTypeWrites: Writes[ShipmentType] =Json.writes[ShipmentType]
  </code></pre>
  
  <p>However this fails too,  as I get errors like: "No unapply function found". 
  Anyone an idee how to implement a Writer for this? Preferably in the form of a string in json.</p>