Question

I use Play Framework 2.2.1 and reactivemongo with plugin "org.reactivemongo" %% "play2-reactivemongo" % "0.10.2"

My model:

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 is a 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]
}

When I call Template.findAll with data

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

there is an execution exception: [RuntimeException: JsError(List((/sections(0)/section,List(ValidationError(error.path.missing,WrappedArray())))))]

but it works with empty sections: {"name": "Caption", "sections": [], "marks": []}

Is this driver supports recoursive structures?

Was it helpful?

Solution

Well. I've solved it as rename filed sections to parts:

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])

Collection:

{
    "name": "Caption",
    "sections": [{
        "name": "Section name",
        "index": 1,
        "indexTree": "1",
        "marks": [],
        "parts": [{
           ...
           "parts": [...]
        }, ...]
    }],
    "marks": []
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top