I really recommend updating to play 2.1-RC1, because here the JSON authors / readers are very easy to define (more info here )
But to help you avoid some mistakes, I will give you a hint with the import: - use only this import! (note that json.Reads is not included)
import play.api.libs.json._ import play.api.libs.functional.syntax._ import play.api.libs.json.Writes._
and you only need to write this code to write / read your class to / from Json (of course, you will have User instead of Address :
implicit val addressWrites = Json.writes[Address] implicit val addressReads = Json.reads[Address]
Now they will be used automatically:
Write example:
Ok(Json.toJson(entities.map(s => Json.toJson(s))))
Read example (I put my POST execution example to create an object by reading json from the body), please pay attention to addressReads here
def create = Action(parse.json) { request => request.body.validate(addressReads).map { entity => Addresses.insert(entity) Ok(RestResponses.toJson(RestResponse(OK, "Succesfully created a new entity."))) }.recover { Result => BadRequest(RestResponses.toJson(RestResponse(BAD_REQUEST, "Unable to transform JSON body to entity."))) } }
In conclusion, they tried (and succeeded) to make things very simple with respect to JSON.
source share