JSON格式是否不足以解析具有>;22场?

JSON格式是否不足以解析具有>;22场?,json,scala,playframework,Json,Scala,Playframework,我的案例类有30个字段。为了简单起见,我使用了4个字段 case class Person(id: Long, name: String, age: Int, sex: Sex) val personFormat1: OFormat[(Long, String)] = ((__ \ "id").format[Long] ~ (__ \ "name").format[String]).tupled val personFormat2: OFormat[(Int, Sex)] = ((__ \ "

我的案例类有30个字段。为了简单起见,我使用了4个字段

case class Person(id: Long, name: String, age: Int, sex: Sex)

val personFormat1: OFormat[(Long, String)] = ((__ \ "id").format[Long] ~ (__ \ "name").format[String]).tupled
val personFormat2: OFormat[(Int, Sex)] = ((__ \ "age").format[Int] ~ (__ \ "sex").format[Sex]).tupled
implicit val personFormat: Format[Person] = (personFormat1 ~ personFormat2)({
  case ((id, name), (age, sex)) => new Person(id, name, age, sex)
}, (person: Format) => ((person.id, person.name), (person.age, person.sex)))
但是,即使在使用format1作为22个字段的一组和format2作为8个字段的一组编写formatter之后,我在尝试解析这个case类的json时仍然会出错

错误是 找不到类型Person的Json序列化程序作为JsObject。尝试为此类型实现隐式OWrites或OFormat


如何编写隐含的Owrites或OFormat?或者如何解决此问题?

您需要一个隐式编写器来完成此操作。像这样的

implicit val person= Json.format[Person]
此外,如果您正在使用自定义数据类型,如您的案例Sex,则需要指定读写器。对于Int、Long、String等基本类型,不需要这样做

  def enumReads[T <: Enum[T]](mkEnum: String => T) = new Reads[T] {
    override def reads(json: JsValue): JsResult[T] =
      json match {
        case JsString(s) => try {
          JsSuccess(mkEnum(s))
        }
        catch {
          case e: IllegalArgumentException =>
            JsError("Not a valid enum value: " + s)
        }
        case v => JsError("Can't convert to enum: " + v)
      }
  }

  implicit val enumWrites = new Writes[Enum[_]] {
    def writes(e: Enum[_]) = JsString(e.toString)
  }

  implicit val sex = enumReads(Sex.valueOf)
def enumReads[T)=新读取[T]{
重写def读取(json:JsValue):JsResult[T]=
json匹配{
case JsString=>try{
JsSuccess(mkEnum)
}
抓住{
案例e:IllegalArgumentException=>
JsError(“不是有效的枚举值:”+s)
}
案例v=>JsError(“无法转换为枚举:+v”)
}
}
隐式val enumWrites=新写入[Enum[]]{
def写入(e:Enum[])=JsString(e.toString)
}
隐式val sex=enumReads(sex.valueOf)

此外,请升级到scala 2.11或更高版本,以避免case类中22个字段的限制。有关更多信息,请参阅此处:

我使用播放Json扩展库来处理超过22个字段的Json:


我在谷歌上发现了这一点,这对我来说非常有用

// https://mvnrepository.com/artifact/com.chuusai/shapeless_2.11
libraryDependencies += "com.chuusai" % "shapeless_2.11" % "2.3.2"

// https://mvnrepository.com/artifact/org.julienrf/play-json-derived-codecs_2.11
libraryDependencies += "org.julienrf" % "play-json-derived-codecs_2.11" % "3.2"

我想,你误解了我的问题。我的case类有30个字段,超过22个字段。因此,我编写了一个自定义格式设置程序,您可以在所讨论的示例中看到。因此,即使在使用此自定义格式设置程序之后,我也会因为没有json序列化程序而收到一个错误。谢谢。但我尝试了此方法。json中没有用于反序列化/序列化的写入/读取方法-扩展。如何实现这一点?如果你能向我指出这一点,这将非常有用。你只能使用默认格式,好吧,你可以用一个字段进行一些转换,比如用
SingletonEncoder
进行大写,但不能再多了。“dīvide etīmpera”我想这是最好的策略。我在开始使用play时就开始使用
play json extensions
,但上次使用它是很久以前的事了,我没有完成一项任务,因为它无法将json分成更小的部分。
// https://mvnrepository.com/artifact/com.chuusai/shapeless_2.11
libraryDependencies += "com.chuusai" % "shapeless_2.11" % "2.3.2"

// https://mvnrepository.com/artifact/org.julienrf/play-json-derived-codecs_2.11
libraryDependencies += "org.julienrf" % "play-json-derived-codecs_2.11" % "3.2"