So, I have a case class as well as readers and writers for both JSON and BSONDocument format.
Problem is, when inserting to MongoDB, I want to be able to specify the BSONObjectID, so I can return it upon creation. However, if I add a id: BSONObjectID
in the case class, I cannot find a way to get the JSON validation/transformation to work.
This is my code:
case class Mini(username: String, email: String, quizAnswer1: List[String] )
implicit object MiniWriter extends BSONDocumentWriter[Mini] {
def write(mini: Mini): BSONDocument = BSONDocument(
"username" -> mini.username,
"email" -> mini.email,
"quizAnswer1" -> mini.quizAnswer1
)
}
implicit object MiniReader extends BSONDocumentReader[Mini] {
def read(doc: BSONDocument): Mini = Mini(
doc.getAs[String]("username").get,
doc.getAs[String]("email").get,
doc.getAs[List[String]]("quizAnswer1").toList.flatten
)
}
implicit val miniReads: Reads[Mini] = (
(JsPath \ "username").read[String] and
(JsPath \ "email").read[String] and
(JsPath \ "quizAnswer1").read[List[String]]
)(Mini.apply _)
implicit val miniWrites: Writes[Mini] = (
(JsPath \ "username").write[String] and
(JsPath \ "email").write[String] and
(JsPath \ "quizAnswer1").write[List[String]]
)(unlift(Mini.unapply))
I really want to avoid working with duplicate model representations of the same model. Any ideas?