[2.0] Trouble with implicit Json Format objects

Showing 1-5 of 5 messages
[2.0] Trouble with implicit Json Format objects oblivion 2/14/12 11:58 AM
Hi,

I tried to move my Format objects to a package object to get rid of
the clutter in my controllers but the compiler throws the following
error:

No Json deserializer found for type List[Drawing]. Try to implement an
implicit Writes or Format for this type.

So the compiler does not find the object once it is moved out of the
controller where I convert my json.

I thought that a package-object would be the ideal place to put these
things.

Where else can I put them, then?
Re: Trouble with implicit Json Format objects oblivion 2/14/12 12:02 PM
Never mind, it works now, I defined a false package statement.

Thanks for the RC !
Re: Trouble with implicit Json Format objects Jay 3/6/12 11:04 AM
I'm having the same issue, but my Format objects are in the same package and it won't compile "No Json deserializer found for type List[models.Role]. Try to implement an implicit Reads or Format for this type."  How did you resolve this?  My code is below

User.scala 
===============

case class User(username: String, password: String, role: List[Role]) extends MongoModel[User] {

.....

  implicit object UserFormat extends Format[User] {    

    def reads(json: JsValue): User = User(

      (json \ "id").as[String],

      (json \ "password").as[String],

      (json \ "role").asOpt[List[Role]].getOrElse(List()))


    //unmarshaling to JSValue is covered in the next paragraph

    def writes(u: User): JsValue = JsObject(Seq(

      "username" -> JsString(u.username),

      "password" -> JsString(u.password),

      "role" -> JsArray(u.role.map(toJson(_)))))

  }


Role.scala
==================

case class Role(name: String, value: Int) extends MongoModel[Role]{

  implicit object RoleFormat extends Format[Role] {

    def reads(json: JsValue): Role = Role(

      (json \ "name").as[String],

      (json \ "value").as[Int])

    //unmarshaling to JSValue is covered in the next paragraph

    def writes(r: Role): JsValue = JsObject(Seq(

      "name" -> JsString(r.name),

      "value" -> JsNumber(r.value)))

  }

}


Thanks.


On Tuesday, February 14, 2012 1:02:36 PM UTC-7, oblivion wrote:
Never mind, it works now, I defined a false package statement.

Thanks for the RC !
Re: [play-framework] Re: Trouble with implicit Json Format objects Julien Richard-Foy 3/7/12 1:09 AM
The UserFormat and RoleFormat objects must be available in the
implicit scope of the call site.

It may be simpler to put them in an object (instead of in classes) and
to just import them statically.

Re: [play-framework] Re: Trouble with implicit Json Format objects danielkol 3/7/12 1:34 PM
Following the example on the site, I was able to get this to work by making the UserFormat a member of object User instead of case class User