Scala case having 22 fields but having issue with play-json in scala 2.11.5

前端 未结 2 1590
Happy的楠姐
Happy的楠姐 2020-12-05 04:31

With Scala 2.11, we are allowed to have more then 22 fields in a case class right??

case class SomeResponse(
                                     var composi         


        
2条回答
  •  心在旅途
    2020-12-05 05:13

    To make the example above compile, I had to make the type explicit:

    import play.api.libs.json._
    import play.api.libs.functional.syntax._
    
    // Let's pretend this is huge:
    case class Huge(a: Int, b: String, c: Boolean, d: List[Int])
    
    object Huge {
      val fields1to2: Reads[(Int, String)] = (
        (__ \ "a").read[Int] and
        (__ \ "b").read[String]
      ).tupled
    
      val fields3to4: Reads[(Boolean, List[Int])] = (
        (__ \ "c").read[Boolean] and
        (__ \ "d").read[List[Int]]
      ).tupled
    
      val f: ((Int, String), (Boolean, List[Int])) => Huge = {
        case ((a, b), (c, d)) => Huge(a, b, c, d)
      }
    
      implicit val hugeCaseClassReads: Reads[Huge] = (
        fields1to2 and fields3to4
      ) { f }
    
    }
    

提交回复
热议问题