question about SHtml.jsonCall

210 views
Skip to first unread message

Diego Medina

unread,
Jul 8, 2011, 11:56:32 PM7/8/11
to Lift
Hi,

So, the other night I refactored some ajaxCall's for jsonCall's. It
all works well but I still have one problem.

Let's say I have:

"href=#1 [onclick]" #> SHtml.jsonCall(
JsRaw(""" {id : "1"} """) , updateCity _)._2

The type of data updateCity gets is Any. But if I println the value
inside updateCity, it is actually a Map

But to access the values from that Map I ended up adding this:


import net.liftweb.json._
implicit val formats = DefaultFormats
val j: JValue = Extraction.decompose(x)

And then I do a pattern match to extract the json value I passed on
jsonCall. This feels like I'm serializing/deserializing/ several
times. IS this really the way to do it? Or how can I convert the Any
to the Map that it really is?

Thanks

Diego

--
Diego Medina
Web Developer
(305) 788-4954
di...@fmpwizard.com
http://www.fmpwizard.com

Diego Medina

unread,
Jul 10, 2011, 10:27:39 PM7/10/11
to Lift
Well, even though people are usually discouraged from using
asInstanceOf[T], this is what I ended up using.


So my code is now just:


def updateCity(x: Any) : JsCmd = {
val data= x.asInstanceOf[Map[String, String]]

val cometName= data("cometName")
val cityId= data("cityId")
...
}

But if anyone has a better idea, please let me know.

Thanks

Diego

David Pollak

unread,
Jul 11, 2011, 7:23:06 PM7/11/11
to lif...@googlegroups.com
Diego,

I seem to recall that there's an API call to take the old JSON parsed Any and convert it into a JValue.  Maybe someone out there knows the actual API call.

Also, please open a ticket to add JValue versions of jsonCall to SHtml and assign the ticket to me.

Thanks,

David

--
You received this message because you are subscribed to the Google Groups "Lift" group.
To post to this group, send email to lif...@googlegroups.com.
To unsubscribe from this group, send email to liftweb+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.




--
Lift, the simply functional web framework http://liftweb.net

Diego Medina

unread,
Jul 11, 2011, 11:40:47 PM7/11/11
to lif...@googlegroups.com
Thanks David,

grep to the rescue and I think I found the API you were referring to, I now use:


val (cometName: String, cityId) = Full(x).asA[Map[String, Any]] match {
case Full(m) => (
m.get("cometName").getOrElse("No comet Name"),
m.get("cityId").getOrElse("1")
)
case _ => ("No Comet Name", "1")
}

At least some spec tests for the JSONParser object used asA ... Oh
wait, you said to take the old JSON and convert it to JValue, so maybe
this is not it, do you think asA is any better/worse than asInstanceOf
? ()

I'll enter the ticket to add JValue versions of jsonCall in a few minutes

Thanks

Diego

Diego Medina

unread,
Jul 11, 2011, 11:55:00 PM7/11/11
to lif...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages