Scala序列化对象或任何对象的序列

时间:2018-08-28 14:20:54

标签: scala playframework playframework-2.0

我在序列化模型时遇到了一个问题: MyModel:

case class DocumentModel(
                          documentId: Option[Int] = None,
                          title: String,
                          ...
                          responsibleList: Option[Seq[UserModel]], // No problem
                          preConditions: Option[Seq[Object]], // Problem occurs here
                          postConditions: Option[Seq[Object]] // Problem occurs here
                        )

object DocumentModel {
  implicit val documentFormat = Jsonx.formatCaseClass[DocumentModel]
}

responsibleListpreConditionspostConditions未存储在数据库中,它们仅用作帮助程序列表。我继续在控制器中填写这些列表,以便通过JSON将其发送回前端:

val document = for {
  newDocument <- documentDTO.getDocument(documentId, clientId)
  responsibleList <- raciDocumentDTO.getResponsible_Documents(documentId)
  ...
  preConditions <- conditionController.getPreConditions(documentId)
  postConditions <- conditionController.getPostConditions(documentId)
} yield (newDocument, responsibleList, ..., preConditions, postConditions)

document.map({ case (newDocument, responsibleList, ..., preConditions, postConditions) =>
  val temp = newDocument.map(docu => {
    ...

    val doc = new DocumentModel(
      docu.documentId,
      docu.title,
      ...
      Some(responsibleList),
      Some(preConditions),
      Some(postConditions)
    )
    doc
  })

  Ok(Json.obj(
    "document" -> temp,
  ))
})

我怀疑这与Option[Seq[Object]]Option[Seq[Any]]有关。编译器将引发错误: enter image description here 由于preConditionspostConditions可以是来自各种instances/objects的{​​{1}},因此我无法明确说明其应为哪种模型。是否有解决此问题的方法,还是我必须走另一条路? 预先感谢!

更新

我添加了:

models

在我的模型中:

trait Condition

object Condition {
  import play.api.libs.json._

  implicit val conditionWrites: Writes[Condition] = Writes[Condition] {
    case a: DocumentModel => JsObject(Seq("DocumentModel" -> Jsonx(a)))
    case b: ProcessModel => JsObject(Seq("ProcessModel" -> Jsonx(b)))
    case c: ProcessInstanceModel => JsObject(Seq("ProcessInstanceModel" -> Jsonx(c)))
  }
}

它仍然让我看到此错误消息:

enter image description here

enter image description here

那是我第一次遇到这样的问题,老实说,我有点困惑。

更新2

我已经像您提到的那样尝试过,但是它仍然抛出相同的错误。

我的代码如下:

enter image description here

更新

我实施了建议的更改,然后将问题缩小为:

case class DocumentModel(
                          documentId: Option[Int] = None,
                          title: String,
                          ...,
                          responsibleList: Option[Seq[UserModel]],
                          preConditions: Option[Seq[Condition]],
                          postConditions: Option[Seq[Condition]]
                        ) extends Condition

当我这样做时,会收到以下错误消息:

enter image description here 因此,我认为可能缺少trait Condition object Condition { implicit val conditionWrites: Writes[Condition] = Writes[Condition] { case a: DocumentModel => JsObject(Seq( "documentId" -> Json.toJson(a.documentId), "title" -> Json.toJson(a.title), ... "responsibleList" -> Json.toJson(a.responsibleList), "preConditions" -> Json.toJson(a.preConditions), "postConditions" -> Json.toJson(a.postConditions) )) case b: ... } } 。所以我加了:

implicit read

但是,在运行代码时我得到了:

object Condition {
  implicit val conditionReads: OFormat[Condition] = Json.format[Condition] // New

  implicit val conditionWrites: Writes[Condition] = Writes[Condition] {
    case a: DocumentModel => JsObject(Seq(
      "documentId" -> Json.toJson(a.documentId),
      "title" -> Json.toJson(a.title),
      ...

解决此问题的建议解决方案是将我的 No unapply or unapplySeq function found [error] implicit val conditionReads: OFormat[Condition] = Json.format[Condition] 更改为object condition。但是,这将破坏创建此类通用case class condition的全部目的。

1 个答案:

答案 0 :(得分:2)

让您所有的“条件”模型扩展具有JSON可写特征。例如,

trait Condition

object Condition {

  implicit val conditionWrites: Writes[Condition] = Writes[Condition] {
    case a: PreConditionA => JsObject(Seq("field" -> JsString(a.stringValue))) //or whatever
    case b: PreConditionB => //...
    //etc...
    case x: PostConditionX => //...
    case y: PostConditionY => //...
    //etc...
  }

}

case class PreConditionA(stringValue: String) extends Condition
//etc.

然后在DocumentModel中,将条件的类型更改为Condition特征:

case class DocumentModel(
  documentId: Option[Int] = None,
  title: String,
  //...
  responsibleList: Option[Seq[UserModel]], 
  preConditions: Option[Seq[Condition]], 
  postConditions: Option[Seq[Condition]]
)

您可能必须将序列化语法转换为Jsonx的语法,但希望它足够相似,您会明白的。