在Scala 2.11.5中,有一个包含22个字段的Scala case,但在play-json中出现了问题。

29

使用Scala 2.11,我们可以在case类中拥有超过22个字段吗?

case class SomeResponse(
                                     var compositeKey: String,
                                     var id1: String,
                                     var id2: String,
                                     var firstName: String,
                                     var lastName: String,
                                     var email: String,
                                     var email2: String,
                                     var birth: Long,
                                     var gender: String,
                                     var phone: Phone,
                                     var city: String,
                                     var zip: String,
                                     var carriage: Boolean,
                                     var carriage2: Boolean,
                                     var fooLong: Long,
                                     var fooLong2: Long,
                                     var suspended: Boolean,
                                     var foo: Foo,
                                     var address: String,
                                     var suite: String,
                                     var state: String,
                                     var instructions: String)

implicit val formatSomeResponse = Json.format[SomeResponse]

好的,上面是一个有22个字段采用play-json格式的case类,在我编译时出现了以下错误:

SomeFile.scala:126: value apply is not a member of play.api.libs.functional.FunctionalBuilder[play.api.libs.json.OFormat]#CanBuild22[String,String,String,String,String,String,String,Long,String,com.Phone,String,String,Boolean,Boolean,Long,Long,Boolean,com.Foo,String,String,String,String]

而且Phone和Foo这两个case class,每个都有两个字段。

那么,为什么我实际上会面临这个问题,它没有超过22个字段的限制,或者我做错了什么其他的事情,我已经在scala 2.11.5 / 2.11.1 - play-json 2.3中尝试过了。

更新: 根据James和Phadej的回答。

  val someResponseFirstFormat: OFormat[(String, String, String, String, String, String, String, Long, String, Phone, String)] =
    ((__ \ "compositeKey").format[String] and
      (__ \ "id1").format[String] and
      (__ \ "id2").format[String] and
      (__ \ "firstName").format[String] and
      (__ \ "lastName").format[String] and
      (__ \ "email").format[String] and
      (__ \ "email2").format[String] and
      (__ \ "birth").format[Long] and
      (__ \ "gender").format[String] and
      (__ \ "phone").format[Phone] and
      (__ \ "city").format[String]).tupled

  val someResponseSecondFormat: OFormat[(String, Boolean, Boolean, Long, Long, Boolean, Foo, String, String, String, String)] =
    ((__ \ "zip").format[String] and
      (__ \ "carriage").format[Boolean] and
      (__ \ "carriage2").format[Boolean] and
      (__ \ "fooLong").format[Long] and
      (__ \ "fooLong2").format[Long] and
      (__ \ "suspended").format[Boolean] and
      (__ \ "foo").format[Foo] and
      (__ \ "address").format[String] and
      (__ \ "suite").format[String] and
      (__ \ "country").format[String] and
      (__ \ "instructions").format[String]).tupled

  implicit val formatSome: Format[SomeResponse] = (
    someResponseFirstFormat and someResponseSecondFormat
    ).apply({
    case ((compositeKey, id1, id2, firstName, lastName, email, email2, birth, gender, phone, city),
    (zip, carriage, carriage2, created, updated, suspended, foo, address, suite, country, instructions)) =>
      SomeResponse(compositeKey, id1, id2, firstName, lastName, email, email2, birth, gender, phone, city, zip, carriage, carriage2, created, updated, suspended, location, address, suite, country, instructions)
  }, huge => ((huge.compositeKey, huge.id1, huge.id2, huge.firstName, huge.lastName, huge.email, huge.email2, huge.birth, huge.gender, huge.phone, huge.city),
    (huge.zip, huge.carriage, huge.carriage2, huge.created, huge.updated, huge.suspended, huge.foo, huge.address, huge.suite, huge.country, huge.instructions)))

听起来这个问题与Scala无关,而是与play-json有关。猜测可能是因为Play仍然需要支持Scala 2.10,所以Play仍然遵守了限制。 - lmm
是的,在这个SO帖子中有解释https://dev59.com/j2Ag5IYBdhLWcg3wdauF,但问题是我还没有超过“超过22”的限制。因此,根据play-json,它需要少于22个字段? - mane
2
这一定是地狱... - Nanocom
2个回答

22

你可以将Reads定义进行拆分:

val fields1to10: Reads[(A,B,C,D,E,F,G,H,I,J)] = ???
val fields11to20 = ???
val fields21to30 = ???

implicit val hugeCaseClassReads: Reads[HugeCaseClass] = (
  fields1to10 and fields11to20 and fields21to30
) { a, b, c => createHugeCaseClassFromThreeTuples(a, b, c) }
"函数式语法"不能处理超过22个字段的原因是因为只有在22个字段之前定义了中间类:FunctionalBuilder"
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])

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

implicit val hugeCaseClassReads: Reads[Huge] = (
  fields1to2 and fields3to4
) {
  case ((a, b), (c, d)) =>  
    Huge(a, b, c, d)
}

尝试验证 null 的结果:

scala> JsNull.validate[Huge]
res6: play.api.libs.json.JsResult[Huge] = JsError(
  List(
    (/b,List(ValidationError(error.path.missing,WrappedArray()))),
    (/d,List(ValidationError(error.path.missing,WrappedArray()))),
    (/c,List(ValidationError(error.path.missing,WrappedArray()))),
    (/a,List(ValidationError(error.path.missing,WrappedArray())))))

如您所见,所有字段都已经尝试。


或者,您可以使用更多的 CanBuildNN 类扩展play:https://github.com/playframework/playframework/blob/2.3.6/framework/src/play-functional/src/main/scala/play/api/libs/functional/Products.scala


然而,我建议您将字段分组为SomeResponse类,例如与地址相关的等等。如果JSON结构是平面的且无法更改,则手动编写ReadsWrites实例。


1
你能提供一个使用示例吗?我尝试按照您建议的方法操作,但无法得到正确的答案。 - mane
1
CanBuild22已经在主分支上提供了,无法构建CanBuild23及更高版本,因为它们需要Tuple23和Function23,而这两个函数不存在。 - James Roper
1
@JamesRoper 他们不能由用户定义吗?还是有一些JVM的限制? - phadej
@phadej 我能在play.api.libs.json.Format上使用这种方式吗?我得到了一个错误“value and is not a member of play.api.libs.json.Format”。 - angelokh
@angelokh,是的。但你必须手写FormatReads(如上所述)和Writes(可以直接编写)部分。 - phadej
显示剩余10条评论

14
为了让上面的例子能够编译通过,我必须将类型明确指定:
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 }

}

2
你能否提供一个OWrites/OFormat的完整示例?这里有一个链接,但在2.5.9+上无法编译。 - JMess

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接