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
You can define
Format[Page[T]]
for generic case classPage[T]
like this:Although this solution requires more typing, it keeps your case class
Page[T]
clear of implicit parameter list or need to define concrete subclasses ofPage[T]
.maybe you can write something like:
Put the JsValue instead of the Seq[T]
here is my code example:
I'd prefer this solution with
trait
, but in case you do want to make your case class generic you could use one of 2 approaches.In case you don't have to use
Page[_]
, i.e. you'll always calltoJson
onPage[Int]
orSeq[Page[String]]
, but not onPage[_]
orSeq[Page[_]]
:In case you have to serialize
Page[_]
:I don't think that you can have a generic writer for any type parameter. I propose following: