Skip to content

Instantly share code, notes, and snippets.

@benoit-ponsero
Created February 27, 2014 19:55
Show Gist options
  • Save benoit-ponsero/9258130 to your computer and use it in GitHub Desktop.
Save benoit-ponsero/9258130 to your computer and use it in GitHub Desktop.
replace _id:BsonObjectId by id:String in play reactive mongo
import play.api.libs.json._
case class User (id:String, name:String)
object JsonFormats {
def mongoReads[T](r: Reads[T]) = {
__.json.update((__ \ 'id).json.copyFrom((__ \ '_id \ '$oid).json.pick[JsString] )) andThen r
}
def mongoWrites[T](w : Writes[T]) = {
w.transform( js => js.as[JsObject] - "id" ++ Json.obj("_id" -> Json.obj("$oid" -> js \ "id")) )
}
implicit val userRead: Reads[User] = mongoReads[User](Json.reads[User])
implicit val userWrites: Writes[User] = mongoWrites[User](Json.writes[User])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment