JsValue JsObject and JsValueWrapper

5,223 views
Skip to first unread message

mkr...@trialfire.com

unread,
Jun 25, 2013, 3:49:24 PM6/25/13
to play-fr...@googlegroups.com
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

Julien L.

unread,
Jun 25, 2013, 3:59:39 PM6/25/13
to play-fr...@googlegroups.com
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(...)



mkr...@trialfire.com

unread,
Jun 25, 2013, 9:59:40 PM6/25/13
to play-fr...@googlegroups.com
Thanks. That makes sense and I check out the source code... however I'm still unclear about JsValueWrapper and how it fits in.

Pascal Voitot Dev

unread,
Jun 26, 2013, 2:56:39 AM6/26/13
to play-fr...@googlegroups.com
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.
 
 

mkr...@trialfire.com

unread,
Jun 26, 2013, 9:51:28 AM6/26/13
to play-fr...@googlegroups.com
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

Pascal Voitot Dev

unread,
Jun 26, 2013, 9:53:15 AM6/26/13
to play-fr...@googlegroups.com
Yes exactly and this conversion to JsValueWrapper requires a Writes[T]... So all types with a Writes[T] should work...

Pascal



-Max

--
Reply all
Reply to author
Forward
0 new messages