Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala案例有22个字段,但在Scala 2.11.5中存在播放json的问题_Scala_Playframework_Playframework 2.3 - Fatal编程技术网

Scala案例有22个字段,但在Scala 2.11.5中存在播放json的问题

Scala案例有22个字段,但在Scala 2.11.5中存在播放json的问题,scala,playframework,playframework-2.3,Scala,Playframework,Playframework 2.3,使用Scala 2.11,我们可以在一个case类中有超过22个字段,对吗 case class SomeResponse( var compositeKey: String, var id1: String, var id2: String,

使用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]
上面是一个case类,它正好有22个字段,使用play json格式,现在当我编译时,我得到以下错误:

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]
case类Phone和Foo各有两个字段

那么,为什么我真的要面对这个问题,它没有超过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)))

您可以拆分
读取的
定义:

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个:


为小示例完整编写,如下所示:

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
类扩展游戏:



然而,我建议您将
SomeResponse
类中的字段分组,例如地址相关等,如果JSON结构是扁平的且无法更改,则手动写入
读取
写入
实例。

要编译上述示例,我必须使类型显式:

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 }

}

听起来这是play json的问题,而不是scala的问题。完全可以猜测,如果play仍然必须支持scala 2.10,那么play可能仍然遵守该限制?是的,这篇文章对此进行了解释,但问题是我没有超过“超过22”的限制。所以,根据play json,它需要小于22?这一定是地狱…你能提供一个使用示例吗,我试着按照你的建议做,但无法得到正确的答案CanBuild22已经在master上提供了,CanBuild23和更大的版本是不可能的,因为它们需要Tuple23和Function23,它们不存在。@JamesRoper用户不能也定义它们吗?或者有JVM限制吗?我不确定,也许有可能。虽然这对case类没有帮助,但Scala编译器不会在包含22个以上字段的case类伴生对象上生成apply/unapply方法,您是否还必须自己手动实现它们才能使play json宏正常工作。@JamesRoper这就是我建议对字段进行分组的原因,所以你们不会超过22个极限,甚至接近它:)你们能给一个完整的例子,一个OWrites/OFormat吗?有一个,但它没有在2.5.9上编译+