0

我使用 Play Framework 2.2.1 和 reactivemongo 插件 "org.reactivemongo" %% "play2-reactivemongo" % "0.10.2"

我的模型:

package models

import system.db.Mongo
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import play.api.libs.json.Json
import scala.concurrent.Awaitable
import scala.concurrent.duration._
import scala.concurrent.Await

object Template {
  import system.db.Mongo.JsonFormats._
  def findAll = Await.result(Mongo.templates.find(Json.obj()).cursor[Template].collect[List](), 3 seconds)
}

case class Template(name: String, marks: List[Mark], sections: List[Section])

case class Mark(name: String, description: String, index: Int, indexTree: String, dataType: String, rate: Int)

case class Section(name: String, index: Int, indexTree: String, marks: List[Mark], section: List[Section])

Mongo.templates 是一个 JSONCollection:val templates: JSONCollection = db.collection[JSONCollection]("Templates")

json格式:

object JsonFormats {
    implicit val markFormat = Json.format[Mark]
    implicit val sectionFormat = Json.format[Section]
    implicit val templateFormat = Json.format[Template]
}

当我Template.findAll用数据打电话时

{
    "name": "Caption",
    "sections": [{
        "name": "Section name",
        "index": 1,
        "indexTree": "1",
        "marks": [],
        "sections": []
    }],
    "marks": []
}

有一个执行异常:[RuntimeException: JsError(List((/sections(0)/section,List(ValidationError(error.path.missing,WrappedArray())))))]

但它适用于空部分{"name": "Caption", "sections": [], "marks": []}

这个驱动程序支持递归结构吗?

4

1 回答 1

1

出色地。我已将其解决为将归档部分重命名为部分

case class Template(name: String, marks: List[Mark], sections: List[Section])

case class Mark(name: String, description: String, index: Int, indexTree: String, dataType: String, rate: Int)

case class Section(name: String, index: Int, indexTree: String, marks: List[Mark], parts: List[Section])

收藏:

{
    "name": "Caption",
    "sections": [{
        "name": "Section name",
        "index": 1,
        "indexTree": "1",
        "marks": [],
        "parts": [{
           ...
           "parts": [...]
        }, ...]
    }],
    "marks": []
}
于 2014-02-08T17:35:05.213 回答