我有一个类,我希望能够将其转换为 json:
case class Page[T](items: Seq[T], pageIndex: Int, pageSize: Int, totalCount: Long)
object Page {
implicit val jsonWriter: Writes[Page[_]] = Json.writes[Page[_]]
}
错误是No apply function found matching unapply parameters
您可以定义Format[Page[T]]
对于通用案例类Page[T]
像这样:
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))
尽管此解决方案需要更多输入,但它会保留您的案例类别Page[T]
清除隐式参数列表或需要定义具体的子类Page[T]
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)