playframework 2.1 - Transform _id to id with JSON format macro -
i use reactivemongo in play application. models use property id , not _id. how can automatically transform object id(_id), mapped id property of models.
currently write format hand:
implicit val adviceformat = ( (__ \ '_id).format[bsonobjectid] , (__ \ 'lang).format[lang] , (__ \ 'title).format[string] , (__ \ 'text).format[string] , (__ \ 'reads).formatnullable[seq[periodcounter]] , (__ \ 'creationdate).format[datetime] , (__ \ 'updatedate).format[datetime] )(advice.apply, unlift(advice.unapply)) but write only:
implicit val adviceformat = json.format[advice] update:
based on answer of trevor.reznik have figured 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") )
for json writes can :
val userwrites = json.writes[user].transform( js => js.as[jsobject] - "id" ++ json.obj("_id" -> js \ "id") ) but i'm not sure can similar reads.
Comments
Post a Comment