Created
February 27, 2014 19:55
-
-
Save benoit-ponsero/9258130 to your computer and use it in GitHub Desktop.
replace _id:BsonObjectId by id:String in play reactive mongo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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