Scala案例有22个字段,但在scala 2.11.5中遇到play-json问题

时间:2016-09-27 02:46:25

标签: json scala play-json

我们目前正在使用scala 2.11.5,而我的班级中有1个有22个字段。

  case class CreateTransactionRequest(name: Option[String],
                                  balance: Option[BigDecimal],
                                  amount: BigDecimal,
                                  currency: String,
                                  dueDate: Option[DateTime],
                                  orderDate: DateTime,
                                  billing: Option[CreateAddressRequest],
                                  shipping: Option[CreateAddressRequest],
                                  status: String,
                                  email: String,
                                  paymentMethod: String,
                                  paymentTerm: Option[String],
                                  deliveryMethod: Option[String],
                                  source: String,
                                  attachments: Option[String],
                                  agent: Option[String],
                                  orders: Option[Set[CreateOrderRequest]],
                                  otherCharges: Option[Seq[CreateTransactionOtherChargeRequest]],
                                  notes: Option[Seq[CreateNoteRequest]],
                                  subscribers: Option[Seq[String]],
                                  trackingId: Option[String],
                                  acquisition: Option[String])

implicit val readsCreateTransactionRequest: Reads[CreateTransactionRequest] = Json.reads[CreateTransactionRequest]

环顾四周后,我遇到了一个解决方案并将其应用

val fields1to12: Reads[(Option[String], Option[BigDecimal], BigDecimal, String, Option[DateTime], DateTime, Option[CreateAddressRequest], Option[CreateAddressRequest], String, String, String, Option[String])] = (
    (__ \ "name").read[Option[String]] and
    (__ \ "balance").read[Option[BigDecimal]] and
    (__ \ "amount").read[BigDecimal] and
    (__ \ "currency").read[String] and
    (__ \ "dueDate").read[Option[DateTime]] and
    (__ \ "orderDate").read[DateTime] and
    (__ \ "billing").read[Option[CreateAddressRequest]] and
    (__ \ "shipping").read[Option[CreateAddressRequest]] and
    (__ \ "status").read[String] and
    (__ \ "email").read[String] and
    (__ \ "paymentMethod").read[String] and
    (__ \ "paymentTerm").read[Option[String]]
  ).tupled

  val fields13to22: Reads[(Option[String], String, Option[String], Option[String], Option[Set[CreateOrderRequest]], Option[Seq[CreateTransactionOtherChargeRequest]], Option[Seq[CreateNoteRequest]], Option[Seq[String]], Option[String], Option[String])] = (
    (__ \ "deliveryMethod").read[Option[String]] and
    (__ \ "source").read[String] and
    (__ \ "attachments").read[Option[String]] and
    (__ \ "agent").read[Option[String]] and
    (__ \ "orders").read[Option[Set[CreateOrderRequest]]] and
    (__ \ "otherCharges").read[Option[Seq[CreateTransactionOtherChargeRequest]]] and
    (__ \ "notes").read[Option[Seq[CreateNoteRequest]]] and
    (__ \ "subscribers").read[Option[Seq[String]]] and
    (__ \ "trackingId").read[Option[String]] and
    (__ \ "acquisition").read[Option[String]]
  ).tupled

implicit val readsCreateTransactionRequest: Reads[CreateTransactionRequest] = (fields1to12, fields13to22) {
    case((name, balance, amount, currency, dueDate, orderDate, billing, shipping, status, email, paymentMethod, paymentTerm), (deliveryMethod, source, attachments, agent, orders, otherCharges, notes, subscribers, trackingId, acquisition)) => 
    CreateTransactionRequest(name, balance, amount, currency, dueDate, orderDate, billing, shipping, status, email, paymentMethod, paymentTerm, deliveryMethod, source, attachments, agent, orders, otherCharges, notes, subscribers, trackingId, acquisition)
  }

我确信我已经关注并尝试了link。不幸的是,它不起作用,我仍然有错误

有人可以提出一些其他方法来分割Read上的22个字段吗?

1 个答案:

答案 0 :(得分:1)

虽然未提供完整代码,但我可以在代码中看到一些问题。首先,您正在编写以下代码:

(__ \ "billing").read[Option[CreateAddressRequest]]

此处, CreateAddressRequest 不是基本类型。因此,您需要为 CreateAddressRequest 编写隐式Writes。

然后,案例类的最大字段是可选的。播放无法手动序列化/反序列化可选字段。所以,你需要有以下代码。

implicit def optionFormat[T: Format]: Format[Option[T]] = new Format[Option[T]] {
    override def reads(json: JsValue): JsResult[Option[T]] = json.validateOpt[T]

    override def writes(o: Option[T]): JsValue = o match {
      case Some(t) => implicitly[Writes[T]].writes(t)
      case None    => JsNull
    }
  }

而且,播放也无法序列化/反序列化日期字段。您还需要编写日期的写入。以下是 java.util.Date 写入的示例代码。

implicit object DateFormat extends Format[java.util.Date] {
    val format = new java.text.SimpleDateFormat("yyyy-MM-dd")
    def reads(json: JsValue): JsResult[java.util.Date] = JsSuccess(format.parse(json.as[String]))
    def writes(date: java.util.Date): JsString = JsString(format.format(date))
  }

已解释了一个示例here,您可以阅读我的文章here