Reactivemongo nested JSON structure

672 views Asked by At

I'm currently working on simple project in Play Framework 2.3.6 with reactivemongo "org.reactivemongo" %% "play2-reactivemongo" % "0.10.5.0.akka23" and I have problem with nested structure.

My goal is to map such as JSON structure (to Scala equivalent):

{
    _id: ObjectId,
    url: String,
    description: String,
    visitors: [
        _id: ObjectId,
        isFirstTime: Boolean
    ]
}

My Scala code:

import play.api.libs.json.Json
import play.modules.reactivemongo.json.BSONFormats._
import reactivemongo.bson._
import reactivemongo.bson.BSONObjectID

case class Visitor(_id: BSONObjectID, isFirstTime: Boolean)
case class Project(_id: BSONObjectID, url: String, description: String, visitors: List[Visitor])

object JSONFormats {
    implicit val projectFormat = Json.format[Project]
}

In the last line the following error appears : "No implicit format for List[models.Visitor] available."

How can I fix this?

1

There are 1 answers

0
Ryan On BEST ANSWER

Add a formatter for Visitor. Idiomatically, these should go in the companion objects.

object Visitor {
  implicit val fmt = Json.format[Visitor]
}

object Project {
  implicit val fmt = Json.format[Project]
}