playframework-json

Scala Play Json Reads

别来无恙 提交于 2019-12-04 04:50:11
I have a sample code as below. import play.api.libs.json._ import play.api.libs.functional.syntax._ import play.api.data.validation.ValidationError import play.api.libs.json.Reads._ case class Retailer(firstName:String,lastName:String,email:String,mobileNo:String,password:String) case class Business(name:String,preferredUrl:String,businessPhone:String,retailer:Retailer) object JsonTest { val jsonValue = """ { "business": { "name":"Some Business Name", "preferredUrl":"someurl", "businessPhone":"somenumber", "retailer": { "firstName":"Some", "lastName":"One", "email":"someone@somewhere.com",

serializing objects to json with play.api.libs.json

谁说胖子不能爱 提交于 2019-12-03 06:50:12
问题 I'm trying to serialize some relatively simple models into json. For example, I'd like to get the json representation of: case class User(val id: Long, val firstName: String, val lastName: String, val email: Option[String]) { def this() = this(0, "","", Some("")) } Do i need to write my own Format[User] with the appropriate reads and writes methods or is there some other way? I've looked at https://github.com/playframework/Play20/wiki/Scalajson but I'm still a bit lost. 回答1: Yes, writing your

Custom JSON validation constraints in Play Framework 2.3 (Scala)

旧城冷巷雨未停 提交于 2019-12-03 05:54:29
I managed to implement form validation with custom constraints, but now I want to do the same thing with JSON data. How can I apply custom validation rules to a JSON parser? Example: The client's POST request contains a user name ( username ) and not only do I want to make sure that this parameter is a non-empty text, but also that this user actually exists in the database. // In the controller... def postNew = Action { implicit request => request.body.asJson.map { json => json.validate[ExampleCaseClass] match { case success: JsSuccess[ExampleCaseClass] => val obj: ExampleCaseClass = success

Defining `Reads` for JSON Set Type

﹥>﹥吖頭↗ 提交于 2019-11-28 14:13:50
How can I create a play.api.libs.Reads for my People case class? scala> type Id = Long defined type alias Id scala> case class People(names: Set[Id]) defined class People scala> implicit val PeopleReads: Reads[People] = ( | (__ \ "names").read[Set[Id]])(People) <console>:21: error: overloaded method value read with alternatives: (t: Set[Id])play.api.libs.json.Reads[Set[Id]] <and> (implicit r: play.api.libs.json.Reads[Set[Id]])play.api.libs.json.Reads[Set[Id]] cannot be applied to (People.type) (__ \ "names").read[Set[Id]])(People) The (...)(People) syntax is designed for when you've built up a

Iterating over an array in JSON with Play 2.1.1

北城以北 提交于 2019-11-28 11:43:16
I am using play 2.1.1 and I am having issues iterating through an array. I had read somewhere that you can create a reads for a List[Object] but everytime I try to do this I get an error "No unapply function found" for the line that says implicit val userListReads: Reads[List[FBUser]] = Json.reads[List[FBUser]]". The issue is " = Json.reads[List[FBUser]] I am at a loss of what else to try. Any assistance would be greatly appreciated. def linkUsers() = Action { implicit request => val json = Json.parse("{\"data\": [{\"name\": \"Me Lazyan\",\"id\": \"1182\"},{\"name\": \"Chales Dselle\",\"id\":