加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Scala的案例有22个领域,但在scala中的play-json有问题2.11.5

发布时间:2020-12-16 09:38:57 所属栏目:安全 来源:网络整理
导读:在Scala 2.11中,我们被允许在案例类中有超过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 gend
在Scala 2.11中,我们被允许在案例类中有超过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个字段与播放json格式,现在当我编译,我得到这个错误:

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

而案例类Phone和Foo,每个都有两个字段。

所以,为什么我真的面临这个问题,它不会超过22个领域的限制,还有其他的我做错了,我试过它在scala 2.11.5 / 2.11.1 – play-json 2.3

更新:
根据詹姆斯和Phadej的回答

val someResponseFirstFormat: OFormat[(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,Foo,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,city,zip,location,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)))

解决方法

您可以拆分您的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,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,c,d)
}

而tryint来验证null的结果:

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

你可以看到,所有的字段都被尝试。

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

然而,我建议您在SomeResponse类中对字段进行分组,例如与地址相关的等等。如果JSON结构平坦且无法更改,请手动写入和写入实例。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读