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.--
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.
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?
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
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: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.
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: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.