taken from typesafe's activator template, you can simply use Json.format as an implicit val in the case class's companion object (ReactiveMongo 0.9, scala 2.10.2). example:
package models
import play.api.libs.json.Json
import reactivemongo.bson.BSONObjectID
import play.modules.reactivemongo.json.BSONFormats._
/**
* A message class
*
* @param _id The BSON object id of the message
* @param message The message
*/
case class Message(_id: BSONObjectID, message: String)
object Message {
/**
* Format for the message.
*
* Used both by JSON library and reactive mongo to serialise/deserialise a message.
*/
implicit val messageFormat = Json.format[Message]
}
i am using it, and you can use more parameters as long as JSON knows how to format them, or, if you have members that are case classes you created, if the have the same:
package models
import play.api.libs.json.Json
import reactivemongo.bson.BSONObjectID
import play.modules.reactivemongo.json.BSONFormats._
/**
* A message class
*
* @param _id The BSON object id of the message
* @param message The message
*/
case class Name(fName: String, lName: String, mInitial: String)
object Name {
implicit val nameFormat = Json.format[Name]
}
case class Message(_id: BSONObjectID, message: String, name: Name)
object Message {
/**
* Format for the message.
*
* Used both by JSON library and reactive mongo to serialise/deserialise a message.
*/
implicit val messageFormat = Json.format[Message]
}
i still have not figured a way to do it if you have auxiliary constructors, or if you implement apply(...) in the companion. however, the compiler will alert you to that.