atom feed7 messages in com.googlegroups.liftwebRe: JSON builder
FromSent OnAttachments
MariusApr 5, 2008 2:45 am 
MariusApr 5, 2008 5:54 am 
David PollakApr 5, 2008 5:59 am 
MariusApr 5, 2008 6:06 am 
MariusApr 6, 2008 12:02 am 
David PollakApr 6, 2008 3:11 pm 
MariusApr 6, 2008 10:35 pm 
Subject:Re: JSON builder
From:Marius (mari@gmail.com)
Date:Apr 5, 2008 6:06:12 am
List:com.googlegroups.liftweb

On Apr 5, 3:59 pm, David Pollak <d.@athena.com> wrote:

On Apr 5, 2008, at 5:54 AM, Marius wrote:

Ahhh right ... forgot about all that. Thanks !

I'm not sure the "Scala way" (doing stuff like List(), Array(), etc.) is the right way. It's more verbose than what you've done.

What do folks on the list think? Should we do JSON like Scala does other Array/List/Map structures or should lift be more concise (like the code Marius proposed)?

It strikes me that it'd take Marius 5 minutes to turn his stuff into a JsExp (JavaScript expression) and the code would interoperate with all the other JavaScript stuff that's in lift now.

Well the reason I posted this proposal is that I like the verbosity close to JSON syntax. My only 2 cents

Br's, Marius

On Apr 5, 3:41 pm, "David Pollak" <feed@gmail.com> wrote:

import net.liftweb.http.js._import JsCmd._ import JE._ // JavaScript Expressions

val exp = JsObj("command" -> "say_hello", "params" -> JsArray(1, 2, 3, "Hello", JsArray("moose", "fruitbat")))

println(exp.toJsCmd)

On Sat, Apr 5, 2008 at 2:45 AM, Marius <mari@gmail.com> wrote:

Hi,

Hi,

Scala and Lift provide cool support parsing JSON constructs however I haven't seen an implementation of building JSON constructs from Scala code. Do you know any library to do that?

I was actually looking for Scala syntax that would be similar with JSON constructs. I have build some Scala utility that for given JSON construct:

{"name": "Doe", "phone": "1231231231", "title": "Mr.", "other": {"ocupation": "programmer", "age": 29, "enums": [1, 2, 3, 4, 5]}, "arrayofsomething": [{"1": "2", "3": "4"}, "b": "c"]}

Scala correspondent code is:

val json = ("name" -> "Doe") ~ ("phone" -> "1231231231") ~ ("title" -> "Mr.") ~ ("other" -> ("ocupation" -> "programmer") ~ ("age" -> 29) ~ ("enums" -> Array(1, 2, 3, 4, 5))) ~ ("arrayofsomething" -> Array(("1" -> "2") ~ ("3" -> "4"), ("b" -> "c")))

println(stringify(json))

Do you think that this would be beneficial for Lift code?

Here is the Scala code that allows this style of building JSON constructs:

object JSonBuilder { implicit def some2Assoc(t: Any) = new JSonAssoc(t) implicit def list2Assoc(t: List[Any]) = new JSonListAssoc(t)

def stringify(list : List[T] forSome {type T}): String = { def doStringify(list : List[T] forSome {type T}, sep: String): String = { (list map (_ match { case (left, x : List[Any]) => "\"" + left + "\"" + ": {" + doStringify(x, ", ") + "}" case (left, Seq(i @ _*)) => "\"" + left + "\"" + ": [" + (i map ( _ match { case x: List[Any] => "{" + doStringify(x, ", ") + "}" case e : (l, r) => doStringify(List(e), ", ") case e => e }) mkString(", ")) + "]" case (left, right: String) => "\"" + left + "\"" + ": \"" + right + "\"" case (left, right) => "\"" + left + "\"" + ": " + right toString }) mkString(sep)) } "{" + doStringify(list, ",\n") + "}" }

class JSonAssoc(left: T forSome {type T}) { def ~ (right: P forSome {type P}): List[Q] forSome {type Q} = List(left, right) }

class JSonListAssoc(left: List[T] forSome {type T}) { def ~ (right: P forSome {type P}): List[Q] forSome {type Q} = left ::: List(right) }

}