Let's say I have the following model:
case class Game(var _id: Option[BSONObjectID] = None,
name: String,
genre: Genre.Kind,
var created: Option[DateTime] = None,
var updated: Option[DateTime] = None
)
I want to store an instance of Game
as follows:
{"_id": "ObjectId(12345)", "name": "My Shooter Game", "genre": "Shooter", …}
For Genre
I want to store the value as a String, but programmatically I want to deal with types and not values.
So here is what I thought would work:
object Genre {
sealed trait Kind
implicit val genreJsonFormat = Json.format[Genre.Kind]
case object Shooter extends Kind {
val name = "Shooter"
override def toString = name
}
def getGenre(genre: String) = genre match {
case Shooter.name => Shooter
// ...
}
implicit object GenreWriter extends BSONDocumentWriter[Genre.Kind] {
def write(genre: Genre.Kind): BSONDocument =
BSONDocument("genre" -> genre.toString)
}
implicit object GenreReader extends BSONDocumentReader[Genre.Kind] {
def read(doc: BSONDocument): Genre.Kind = Genre.getGenre(doc.getAs[String]("genre").get)
}
}
Unfortunately, I get this compile error:
No unapply or unapplySeq function found
For:
implicit val genreJsonFormat = Json.format[Genre.Kind]
Any idea how I can achieve that?