Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

overriding method read in trait Reads.

Tags:

json

scala

I'm implementig this User.scala class

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 reads method: overriding method reads in trait Reads of type (json:play.api.libs.json.JsValue)play.api.libs.json.JsResult[models.User];
method reads has incompatible type

I don't want to use a case class (or the problem would be solved), Can anybody give me an Hint on what to do?

Thanks.

like image 755
Tiz Avatar asked Dec 26 '22 07:12

Tiz


1 Answers

You should wrap the "User" object in a JsResult, in this case JsSuccess

JsSuccess(new User(...))
like image 137
Alois Cochard Avatar answered Jan 17 '23 17:01

Alois Cochard