Scala Play Json Reads -
i have sample code 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", "mobileno":"someothernumber", "password":"$^^hfkh*" } } } """ def printjson ={ implicit val rltrds = ( (__ \ "firstname").read[string] ~ (__ \ "lastname").read[string] ~ (__ \ "email").read[string] ~ (__ \ "mobileno").read[string] ~ (__ \ "password").read[string] )(retailer) implicit val bsnsrds = ( (__ \ "name").read[string] ~ (__ \ "preferredurl").read[string] ~ (__ \ "businessphone").read[string] ~ (__ \ "retailer").read[retailer](rltrds) )(business) val buisness = json.parse(jsonvalue).validate[business](bsnsrds) val bus = new business("some business","somebusinessurl","somenumber", new retailer("some","one","someone@somewhere.com","someothernumber","$^^hfkh*")) //val json = json.tojson(bus) println(buisness) } def main(args: array[string]): unit = { printjson } }
i json validation error when try parse json scala object (business class in case). error
jserror(list((/retailer,list(validationerror(validate.error.missing-path,wrappedarray()))), (/preferredurl,list(validationerror(validate.error.missing-path,wrappedarray()))), (/name,list(validationerror(validate.error.missing-path,wrappedarray()))), (/businessphone,list(validationerror(validate.error.missing-path,wrappedarray())))))
however if json
val jsonvalue = """ { "name":"some business name", "preferredurl":"someurl", "businessphone":"somenumber", "retailer": { "firstname":"some", "lastname":"one", "email":"someone@somewhere.com", "mobileno":"someothernumber", "password":"$^^hfkh*" } } """
note outer bracket "{" , "business:" key removed. jssuccess. how write reads json in first case? also, how can in generic way?
please help.
just add business
key in path:
implicit val bsnsrds = ( (__ \ "business" \ "name").read[string] ~ (__ \ "business" \ "preferredurl").read[string] ~ (__ \ "business" \ "businessphone").read[string] ~ (__ \ "business" \ "retailer").read[retailer](rltrds) )(business)
Comments
Post a Comment