playframework-json

how to add custom ValidationError in Json Reads in PlayFramework

放肆的年华 提交于 2021-02-08 05:35:10
问题 I am using play Reads validation helpers i want to show some custom message in case of json exception eg:length is minimum then specified or the given email is not valid , i knnow play displays the error message like this error.minLength but i want to display a reasonable message like please enter the character greater then 1 (or something ) here is my code case class DirectUserSignUpValidation(firstName: String, lastName: String, email: String, password: String) extends Serializable object

how to add alphanumeric field in play framework

ε祈祈猫儿з 提交于 2020-01-07 04:38:08
问题 i need to add alphanumeric field in play for that i am trying this code object TestValidation { implicit val readTestUser: Reads[TestValidation] = ( (JsPath \ "firstName").read(minLength[String](1)) and (JsPath \ "lastName").read(minLength[String](1)) and (JsPath \ "email").read(email) and (JsPath \ "password").read(minLength[String](1)))(TestValidation.apply _) i want the "password" field to be alphanumeric i have added this custom validation constraint now i want to intregate this during

Getting a Play JSON JsValueWrapper for a class that extends a trait

左心房为你撑大大i 提交于 2020-01-04 09:27:11
问题 I'm generating JSON for a speed where the units may vary. I have a SpeedUnit trait and classes that extend it (Knots, MetersPerSecond, MilesPerHour). The JSON Play documentation said "To convert your own models to JsValues, you must define implicit Writes converters and provide them in scope." I got that to work in most places but not when I had a class extending a trait. What am I doing wrong? Or is there an Enum variant I could or should have used instead? // Type mismatch: found (String,

Defining `Reads` for JSON Set Type

半世苍凉 提交于 2019-12-29 01:49:09
问题 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

Defining `Reads` for JSON Set Type

 ̄綄美尐妖づ 提交于 2019-12-29 01:49:04
问题 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

Scala Play Json Reads

折月煮酒 提交于 2019-12-21 12:04:15
问题 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"

How to serialize a Map[CustomType, String] to JSON

China☆狼群 提交于 2019-12-11 03:22:54
问题 Given the following Enumeration ... object MyEnum extends Enumeration { type MyEnum = Value val Val1 = Value("val1") val Val2 = Value("val2") val Val3 = Value("val3") } import MyEnum._ ... and the following Map ... val m = Map( val1 -> "one", val2 -> "two", val3 -> "three" ) ... I need to transform m to JSON: import play.api.libs.json._ val js = Json.toJson(m) The last statement doesn't compile because the compiler doesn't find a Json serializer for type scala.collection.immutable.Map[MyEnum

get all keys of play.api.libs.json.JsValue

非 Y 不嫁゛ 提交于 2019-12-09 13:01:52
问题 I have to store play.api.libs.json.JsValue keys to a List. How i do this? var str = ??? //json String val json: JsValue = Json.parse(str) val data=json.\("data") println(data) //[{"3":"4"},{"5":"2"},{"4":"5"},{"2":"3"}] val newIndexes=List[Long]() expecting newIndexes=List(3,5,4,2) 回答1: If you want to get all keys in the json you can do it recursively with def allKeys(json: JsValue): collection.Set[String] = json match { case o: JsObject => o.keys ++ o.values.flatMap(allKeys) case JsArray(as)

Play2 does not find my implicit Reads or Format for JSON

♀尐吖头ヾ 提交于 2019-12-07 01:25:08
问题 This is my Search Object: package models.helper import play.api.libs.json.Format import play.api.libs.json.JsValue import play.api.libs.json.JsObject import play.api.libs.json.JsString case class Search (name: String, `type`:String){ implicit object SearchFormat extends Format[Search] { def reads(json: JsValue): Search = Search( (json \ "name").as[String], (json \ "type").as[String] ) def writes(s: Search): JsValue = JsObject(Seq( "name" -> JsString(s.name), "type" -> JsString(s.`type`) )) }

Play2 does not find my implicit Reads or Format for JSON

只愿长相守 提交于 2019-12-05 05:43:10
This is my Search Object: package models.helper import play.api.libs.json.Format import play.api.libs.json.JsValue import play.api.libs.json.JsObject import play.api.libs.json.JsString case class Search (name: String, `type`:String){ implicit object SearchFormat extends Format[Search] { def reads(json: JsValue): Search = Search( (json \ "name").as[String], (json \ "type").as[String] ) def writes(s: Search): JsValue = JsObject(Seq( "name" -> JsString(s.name), "type" -> JsString(s.`type`) )) } } I'm trying ot use this class when calling a webservice using WS: val search = response.json.as[Search