JsValue JsObject and JsValueWrapper

Showing 1-6 of 6 messages
JsValue JsObject and JsValueWrapper Max Kremer 6/25/13 12:49 PM
Hi,

  So I'm a new to Play (using 2.1.1) and Scala and I'm a bit confused by the some of the JSON APIs.

For example, I can do this to create a JsObject:

 val person: JsObject = Json.obj(
 
"user" -> "Max",
 
"friend" -> "bob")

and I can do this to create a JsValue

  val json: JsValue = Json.parse("""
 {
  "
name" : "toto",
  "
friend": "dorthy"
 }
 """
)

 both give me similar JSON objects:  {"user":"Max","friend":"bob"} and {"name":"toto","friend":"dorthy"} respectively

But when I try doing something like this to create a JsObject from a JsValue 

 val person: JsObject = Json.obj( json)


I get a compilation exception: type mismatch;  found   : play.api.libs.json.JsValue  required: (String, play.api.libs.json.Json.JsValueWrapper)

JsValueWrapper??? What is that? It doesn't even up when I search for it in the play api docs.

Can someone please point me in the right direction with regards to differentiating JsValue,JsObject and JsValueWrapper


-Max
Re: JsValue JsObject and JsValueWrapper Julien L. 6/25/13 12:59 PM
Hello,

In fact, a JsObject is a JsValue.

case class JsObject(fields: Seq[(String, JsValue)]) extends JsValue

You can use .as[JsObject] or .asOpt[JsObject] to cast your JsValue as a JsObject, or use a more secure method to validate your json as a JsObject.

json.validate[JsObject] // res: JsResult[JsObject] = JsSuccess(...)



Re: JsValue JsObject and JsValueWrapper Max Kremer 6/25/13 6:59 PM
Thanks. That makes sense and I check out the source code... however I'm still unclear about JsValueWrapper and how it fits in.
Re: [play-framework] JsValue JsObject and JsValueWrapper Pascal 6/25/13 11:56 PM



Json.arr and Json.obj are just helpers that Play provides to help people write JS in a more idiomatic way using simple Scala types without needing to write "toto" -> JsString("tata") or "titi" -> JsNumber(3L).

But as you may understand, those helpers need some implicit conversions from simple types behind the curtain and JsValueWrapper is the intermediate structure used to do the conversion from accepted simple types (String, Long, Double, BigDecimal, Float, Boolean, JsValue) to JsValue.

That's why helper function Json.obj(...) only expects (String, JsValueWrapper)* and Json.arr only accepts JsValueWrapper*.

Regards
Pascal
 

-Max

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

Re: [play-framework] JsValue JsObject and JsValueWrapper Max Kremer 6/26/13 6:51 AM
Thanks Pascal.

  So if I understand correctly I can call Json.obj on any type lets say [T] as long as I have an implicit conversion in scope what will convert [T] to an JsValueWrapper?

-Max
Re: [play-framework] JsValue JsObject and JsValueWrapper Pascal 6/26/13 6:53 AM
Yes exactly and this conversion to JsValueWrapper requires a Writes[T]... So all types with a Writes[T] should work...

Pascal



-Max

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