JSON Nested Object

338 views
Skip to first unread message

Amadu Durham

unread,
Apr 23, 2013, 4:28:29 PM4/23/13
to play-fr...@googlegroups.com
Hey,

So I am trying to convert one of my case classes to json in order to add it to a play session. The problem is that the case class is a component of other separate case classes/objects and so I don't think the simple one liner:

json.toJson(object) will work.

It would be really helpful if someone can help me understand what steps I need to take in defining an appropriate class object that can be converted into json. Do I need to define the implicit reads for all subcases/objects? If so how do I do so?

Should it look something like this:
    import play.api.libs.json._
    import play.api.libs.functional.syntax._

    case class X(field1: Y, field2: Z)
    object X extends Function2[Y, Z, Person]{
      implicit val xReads = (
        (__ \ 'field1).read[Y] and
        (__ \ 'field2).read[Z]
      )(X)
    }

and similar for the classes Y and Z, which may also have nested objects that need to be converted into JSON.

I know this may also not work for the fact that a session can only hold 4K of storage, but it's something I think will be useful for the future.

Thank you in advance.

Pascal Voitot Dev

unread,
Apr 23, 2013, 4:33:45 PM4/23/13
to play-fr...@googlegroups.com
I'm not sure to see why you expect to have problems...

case class Y
object Y { implicit val fmt = Json.format[Y] }
case class Z
object Z { implicit val fmt = Json.format[Z] }
case class X( ...:Y, ...:Z)
object X { implicit val fmt = Json.format[X] }

Json.toJson(x:X) should work...

regards
Pascal



--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Amadu Durham

unread,
Apr 23, 2013, 4:56:12 PM4/23/13
to play-fr...@googlegroups.com
What if one of the classes are not a case class? Then would I have to write a custom read/write method for that specific implicit object?

Pascal Voitot Dev

unread,
Apr 23, 2013, 5:21:46 PM4/23/13
to play-fr...@googlegroups.com
On Tue, Apr 23, 2013 at 10:56 PM, Amadu Durham <ama...@gmail.com> wrote:
What if one of the classes are not a case class? Then would I have to write a custom read/write method for that specific implicit object?


yes exactly

 

Amadu Durham

unread,
Apr 24, 2013, 3:11:22 PM4/24/13
to play-fr...@googlegroups.com
Just to be positive, would the custom implementation be similar to the one I posted in the original question, or is this setup on for case classes as well? If so, can you possibly direct me to sources/example of implementing a custom json parser.

Thank You,
Amadu

On Tuesday, April 23, 2013 4:28:29 PM UTC-4, Amadu Durham wrote:

Pascal Voitot Dev

unread,
Apr 24, 2013, 4:31:11 PM4/24/13
to play-fr...@googlegroups.com
yes it's like the code you wrote...
There are more samples in the doc here:
http://www.playframework.com/documentation/2.1.1/ScalaJsonCombinators

Pascal


Amadu Durham

unread,
May 1, 2013, 10:41:55 PM5/1/13
to play-fr...@googlegroups.com
So I am having trouble wrapping my mind around writing composite objects to JSON. I am confused as to what JSON type I should use to write the information, for example, with a string it would be something like the following:

def writes(ts: ConfContext) = JsObject(Seq(

      "viewer" -> JsString(obj.viewer) )

What would be the analogy for other custom objects/types. As I explained before I have objects X, Y,and Z. Objects X is composed of objects Y and Z, so implementing the JSON write method requires me to encapsulate Y and Z into some JSON type, right? How do I do so?

Thank You,

Amadu


On Tuesday, April 23, 2013 4:28:29 PM UTC-4, Amadu Durham wrote:

Pascal Voitot Dev

unread,
May 2, 2013, 2:52:54 AM5/2/13
to play-fr...@googlegroups.com
On Thu, May 2, 2013 at 4:41 AM, Amadu Durham <ama...@gmail.com> wrote:
So I am having trouble wrapping my mind around writing composite objects to JSON. I am confused as to what JSON type I should use to write the information, for example, with a string it would be something like the following:

def writes(ts: ConfContext) = JsObject(Seq(

      "viewer" -> JsString(obj.viewer) )

What would be the analogy for other custom objects/types. As I explained before I have objects X, Y,and Z. Objects X is composed of objects Y and Z, so implementing the JSON write method requires me to encapsulate Y and Z into some JSON type, right? How do I do so?


There are Reads[A] and in the same way, there are Writes[A] that you can compose.
 
case class X(...)
object X { implicit val writer = Json.writes[X] }

case class Y(...)
object Y { implicit val writer = Json.writes[Y] }

case class Z(x:X, y:Y)
object Z {
  implicit val writer = Json.writes[Z]
  // or manually
  implicit val writer = (
    (__ \ "x").write[X] and
    (__ \ "y").write[Y]
  )(Z.apply _)
}

Finally you can use better syntax for JsObject
JsObject(Seq("viewer" -> JsString(obj.viewer) ) ------> Json.obj("viewer" ->...)

Please read the doc, all of this is explained there.

regards
Pascal

Thank You,

Amadu


On Tuesday, April 23, 2013 4:28:29 PM UTC-4, Amadu Durham wrote:
Hey,

So I am trying to convert one of my case classes to json in order to add it to a play session. The problem is that the case class is a component of other separate case classes/objects and so I don't think the simple one liner:

json.toJson(object) will work.

It would be really helpful if someone can help me understand what steps I need to take in defining an appropriate class object that can be converted into json. Do I need to define the implicit reads for all subcases/objects? If so how do I do so?

Should it look something like this:
    import play.api.libs.json._
    import play.api.libs.functional.syntax._

    case class X(field1: Y, field2: Z)
    object X extends Function2[Y, Z, Person]{
      implicit val xReads = (
        (__ \ 'field1).read[Y] and
        (__ \ 'field2).read[Z]
      )(X)
    }

and similar for the classes Y and Z, which may also have nested objects that need to be converted into JSON.

I know this may also not work for the fact that a session can only hold 4K of storage, but it's something I think will be useful for the future.

Thank you in advance.

Amadu Durham

unread,
May 2, 2013, 9:41:11 AM5/2/13
to play-fr...@googlegroups.com
I'm sorry, I just realized I never clarified which version I am working from. I am currently working with Play 2.0.4 and I don't think the syntax import is available in this version, nor is the JSON module that you are using. Is there a plugin/library dependency I can add in order to acquire this functionality? If not, then how do I convert objects to JSON in 2.0.4?

I'm am realizing that updating to the latest version is becoming inevitable, but I'm am not sure if 2.1.x supports Scalate engine and SSPs.


On Tuesday, April 23, 2013 4:28:29 PM UTC-4, Amadu Durham wrote:

Pascal Voitot Dev

unread,
May 2, 2013, 9:53:13 AM5/2/13
to play-fr...@googlegroups.com
On Thu, May 2, 2013 at 3:41 PM, Amadu Durham <ama...@gmail.com> wrote:
I'm sorry, I just realized I never clarified which version I am working from. I am currently working with Play 2.0.4 and I don't think the syntax import is available in this version, nor is the JSON module that you are using. Is there a plugin/library dependency I can add in order to acquire this functionality? If not, then how do I convert objects to JSON in 2.0.4?


in 2.0.4, you must writes Writes manually and call them manually... it's not horrible but it's less neat ;)
 
I'm am realizing that updating to the latest version is becoming inevitable, but I'm am not sure if 2.1.x supports Scalate engine and SSPs.

Don't know exactly about this...
 

On Tuesday, April 23, 2013 4:28:29 PM UTC-4, Amadu Durham wrote:
Hey,

So I am trying to convert one of my case classes to json in order to add it to a play session. The problem is that the case class is a component of other separate case classes/objects and so I don't think the simple one liner:

json.toJson(object) will work.

It would be really helpful if someone can help me understand what steps I need to take in defining an appropriate class object that can be converted into json. Do I need to define the implicit reads for all subcases/objects? If so how do I do so?

Should it look something like this:
    import play.api.libs.json._
    import play.api.libs.functional.syntax._

    case class X(field1: Y, field2: Z)
    object X extends Function2[Y, Z, Person]{
      implicit val xReads = (
        (__ \ 'field1).read[Y] and
        (__ \ 'field2).read[Z]
      )(X)
    }

and similar for the classes Y and Z, which may also have nested objects that need to be converted into JSON.

I know this may also not work for the fact that a session can only hold 4K of storage, but it's something I think will be useful for the future.

Thank you in advance.

Amadu Durham

unread,
May 2, 2013, 10:13:54 AM5/2/13
to play-fr...@googlegroups.com
When trying to write the Write manually I receive a "not found: __" exception, probably because I can not import import play.api.libs.functional.syntax._
How do I solve this problem?

On Tuesday, April 23, 2013 4:28:29 PM UTC-4, Amadu Durham wrote:

Pascal Voitot Dev

unread,
May 2, 2013, 10:18:39 AM5/2/13
to play-fr...@googlegroups.com
__  and functional and Reads[A] and Reads[B] appeared in 2.1.x so not available in 2.0.4

anyway, I also think you should migrate to 2.1.1 if possible (lots of bug corrections and improvements globally)

Pascal


Amadu Durham

unread,
May 8, 2013, 1:46:26 AM5/8/13
to play-fr...@googlegroups.com
I just update my version of the application to play 2.1.1, but now I am having a problem with converting the objects into JSON, because there is a class that I do not have access to, which does not contain an implicit JSON write or read implementation.

Do I have to have access to this custom class/object in order to convert the other composite object to JSON, or are there some work arounds?


On Tuesday, April 23, 2013 4:28:29 PM UTC-4, Amadu Durham wrote:
Reply all
Reply to author
Forward
0 new messages