Unable to convert generic case class to json using “writes”

前端 未结 4 847
时光取名叫无心
时光取名叫无心 2021-02-03 12:01

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         


        
4条回答
  •  清歌不尽
    2021-02-03 12:30

    I don't think that you can have a generic writer for any type parameter. I propose following:

    trait Page[T] {
      val items: Seq[T]
      val pageIndex: Int
      val pageSize: Int
      val totalCount: Long
    }
    
    case class IntPage(items: Seq[Int], pageIndex: Int, pageSize: Int, totalCount: Long) extends Page[Int]
    
    object Page {
      implicit def jsonWriter = Json.writes[IntPage]
    }
    

提交回复
热议问题