The override method read in the Reading attributes.

I am implementing this class User.scala

class User(var id : Long , var name : String) { def createUser() = {} def setName(nome : String) : String = { this.name = nome return name } def getName() : String = { return name } } object User { implicit object userFormat extends Format[User] { override def reads(json: JsValue): User = new User( (json \ "id").as[Long], (json \ "name").as[String] ) override def writes(s: User): JsValue = JsObject(Seq( "id" -> JsString(s.id.toString), "name" -> JsString(s.name) )) } } 

But I get this error when on the reads: method the override is read in the Read type attribute (json: play.api.libs.json.JsValue) play.api.libs.json.JsResult [models.User];
read method has an incompatible type

I do not want to use the case class (or the problem will be solved) Can someone give me a hint about what to do?

Thanks.

+4
source share
1 answer

You must wrap the User object in JsResult, in this case JsSuccess

 JsSuccess(new User(...)) 
+10
source

Source: https://habr.com/ru/post/1469221/


All Articles