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

前端 未结 4 844
时光取名叫无心
时光取名叫无心 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:41

    maybe you can write something like:

        case class Page(items: JsValue, pageIndex: Int, pageSize: Int, totalCount: Long)
    

    Put the JsValue instead of the Seq[T]

    here is my code example:

        case class ResponseObject(state:Int = 1,data:JsValue)
    
        case class City(id:Int,name:String,sort:String){
          require(id > 0)
        }
    
          def list = Action {
            implicit val cityFormat:Format[City] = Json.format[City]
            implicit val responseFormat=Json.format[ResponseObject]
            Ok(Json.toJson(ResponseObject(data=Json.toJson(City.list)))).as("application/json")
          }
    

提交回复
热议问题