The approach I take to decompose JSON data delivered by JsonHandler
(which is in the form of Map[String, _]) is to use extractors that act
as converters, so that the code looks pretty much like in Joni's first
posting.

for{param <- json
  ("messageId", IsInt(messageId)) <- param}
yield ...

object IsInt {
  def unapply(a: String): Option[Int] = try { Some(a.toInt) } catch
{ case _ => None }
}

You can set up nice extractors for all kind of types or to
automatically urldecode or check strings for being not empty and so
on.

-Chris


On 4 Mrz., 21:52, Joni Freeman <[email protected]> wrote:
> Tried it myself and that version does not work. You probably need
> nested for-comprehension which parses the JSON:
>
> for {
>   user <- WorkerMgr.find(UserId(user)) ?~ "User not found"
>   json <- r.json
>   data <- Box(for {
>     JField("messageId", JInt(messageId))  <- json
>     JField("timestamp", JInt(timemillis)) <- json
>   } yield (messageId, timemillis))
>
> } yield ...
>
> Cheers Joni
>
> On Mar 4, 9:55 pm, Joni Freeman <[email protected]> wrote:
>
>
>
> > Hi David,
>
> > Does this work?
>
> >       for {
> >         user <- WorkerMgr.find(UserId(user)) ?~ "User not found"
> >         json <- r.json
> >         JField("messageId", JInt(messageId))  <- json
> >         JField("timestamp", JInt(timemillis)) <- json
> >       } yield ..
>
> > Cheers Joni
>
> > On Mar 4, 9:28 pm, David Pollak <[email protected]> wrote:
>
> > > Folks,
>
> > > I'm working on parsing through some JSON that's posted/put as part of a
> > > request.  I'd like to find the messageId field and only get it if its type
> > > is JInt.  I'm doing this within a for comprehension that has a Box at the
> > > top of it, so the resulting expression must play well with Box.  What I've
> > > got is:
>
> > >       for {
> > >         user <- WorkerMgr.find(UserId(user)) ?~ "User not found"
> > >         json <- r.json
> > >         (_, messageJson) <- (json \ classOf[JField]).find(_._1 ==
> > > "messageId")
> > >         messageId <- Box.asA[JInt](messageJson)
> > >         (_, timemillisJson) <- (json \ classOf[JField]).find(_._1 ==
> > > "timestamp")
> > >         timemillis <- Box.asA[JInt](timemillisJson)
> > >       } yield ...
>
> > > The lines that look for messageId and timestamp and insure they are JInts
> > > are kinda verbose.  Is there a better way to do what I'm trying to do?
>
> > > Thanks,
>
> > > David
>
> > > --
> > > Lift, the simply functional web frameworkhttp://liftweb.net
> > > Beginning Scalahttp://www.apress.com/book/view/1430219890
> > > Follow me:http://twitter.com/dpp
> > > Surf the harmonics

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

Reply via email to