具有递归结构的ReactiveMongoDB插件

时间:2014-02-07 08:00:59

标签: playframework-2.2 reactivemongo

我使用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")

JsonFormats:

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": []}

这个驱动程序是否支持二重结构?

1 个答案:

答案 0 :(得分: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": []
}