播放包含超过22个字段的案例类的json合并格式

问题描述 投票:8回答:2

我试图将格式拆分为多个元组,因此它可以处理案例类中超过22个字段。但是,我收到错误“值,并且不是play.api.libs.json.Format的成员”。如何合并案例类的多种格式?

val fields1to2: Format[(Int, String)] = (
  (__ \ "a").format[Int] and
  (__ \ "b").format[String]
).tupled

val fields3to4: Format[(Boolean, List[Int])] = (
  (__ \ "c").format[Boolean] and
  (__ \ "d").format[List[Int]]
).tupled

implicit val hugeCaseClassReads: Format[Huge] = (
  fields1to2 and fields3to4 // "value and is not a member of play.api.libs.json.Format"
) {
  case ((a, b), (c, d)) =>  
    Huge(a, b, c, d)
}
scala playframework playframework-2.3
2个回答
0
投票

如果您不仅限于使用Play-JSON,请尝试使用Play-Json extensions库:

import ai.x.play.json.Jsonx
implicit val hugeCaseClassReads: Format[Huge] = Jsonx.formatCaseClass

但是一个更方便,安全和有效的选择将是使用jsoniter-scala - 它已经内置支持具有大量字段的案例类。


0
投票

添加这些导入为我解决了这个问题。在您的情况下,只需导入第一个就可以解决问题。

import play.api.libs.functional.syntax._

import play.api.libs.json.{Json, Reads, _}
© www.soinside.com 2019 - 2024. All rights reserved.