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 {
i
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]
.