Unable to convert generic case class to json using "writes"
json, playframework, playframework-2.2, scala
Solution
You can define `Format[Page[T]]` for generic case class `Page[T]` like this:
import play.api.libs.json._
import play.api.libs.functional.syntax._
implicit def pageFormat[T: Format]: Format[Page[T]] =
((__ \ "items").format[Seq[T]] ~
(__ \ "pageIndex").format[Int] ~
(__ \ "pageSize").format[Int] ~
(__ \ "totalCount").format[Long])(Page.apply, unlift(Page.unapply))
Although this solution requires more typing, it keeps your case class `Page[T]` clear of implicit parameter list or need to define concrete subclasses of `Page[T]`.
Problem
I have a class which I want to be able to convert to json: ``` case class Page[T](items: Seq[T], pageIndex: Int, pageSize: Int, totalCount: Long) object Page { implicit val jsonWriter: Writes[Page[_]] = Json.writes[Page[_]] } ``` The error is `No apply function found matching unapply parameters`