I use reactivemongo in my Play application. My models use the property id and not _id. How can I automatically transform the object ID(_id), so that it will be mapped to the id property of my models.
Currently I write the format by hand:
implicit val adviceFormat = (
(__ \ '_id).format[BSONObjectID] and
(__ \ 'lang).format[Lang] and
(__ \ 'title).format[String] and
(__ \ 'text).format[String] and
(__ \ 'reads).formatNullable[Seq[PeriodCounter]] and
(__ \ 'creationDate).format[DateTime] and
(__ \ 'updateDate).format[DateTime]
)(Advice.apply, unlift(Advice.unapply))
But I would like to write only:
implicit val adviceFormat = Json.format[Advice]
Update:
Based on the answer of trevor.reznik I have figured it out.
implicit val adviceJSONReads = __.json.update((__ \ 'id).json.copyFrom((__ \ '_id).json.pick[JsObject] )) andThen Json.reads[Advice]
implicit val adviceJSONWrites = Json.writes[Advice].transform( js => js.as[JsObject] - "id" ++ Json.obj("_id" -> js \ "id") )