XML -> JSON converter?

5 views
Skip to first unread message

harryh

unread,
Sep 2, 2009, 2:27:30 PM9/2/09
to Lift
Is there anything built into lift that will do XML -> JSON
conversion? For example:

<foos>
<foo>
<id>1</id>
<name>Harry</name>
</foo>
<foo>
<id>2</id>
<name>David</name>
</foo>
</foos>

to:

{
"foos": {
"foo": [{
"id": 1,
"name": "Harry"
},
{
"id": 2,
"name": "David"
}
]
}

Just checking to see if there was something Lift friendly to do this
before writing my own (or picking a java library to use).

-harryh

David Pollak

unread,
Sep 2, 2009, 3:26:03 PM9/2/09
to lif...@googlegroups.com
Nothing that I know of but maybe Joni could add it to the new json parse
--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Git some: http://github.com/dpp

Joni Freeman

unread,
Sep 2, 2009, 5:40:27 PM9/2/09
to Lift
Hi,

I just quickly spiked this and following works on my local branch:

scala> val xml =
<foos>
<foo>
<id>1</id>
<name>Harry</name>
</foo>
<foo>
<id>2</id>
<name>David</name>
</foo>
</foos>

scala> val json = toJson(xml)
scala> compact(render(json))
{"foos":{"foo":[{"id":"1","name":"Harry"},{"id":"2","name":"David"}]}}

scala> val json2 = json map {
case JField("id", JString(s)) => JField("id", JInt(s.toInt))
case x => x
}
scala> compact(render(json2))
{"foos":{"foo":[{"id":1,"name":"Harry"},{"id":2,"name":"David"}]}}


This could be quite useful addition. However, I have at least one
concern. What kind of processing instructions will eventually be
needed for this to be generic enough? For instance, lets change the
example XML to:

<foos>
<foo>
<id>1</id>
<name>Harry</name>
</foo>
</foos>

This would still be valid when validating against original XML's DTD,
but the generated JSON structure would be something quite different
(no arrays):
{"foos":{"foo":{"id":1,"name":"Harry"}}}

Cheers Joni

David Pollak

unread,
Sep 2, 2009, 7:40:05 PM9/2/09
to lif...@googlegroups.com
Cool... I'd be interested in you pushing this to master and letting people use it and give feedback.

Joni Freeman

unread,
Sep 3, 2009, 10:43:00 AM9/3/09
to Lift
Ok, added. This is a quick hack just to explore the problem so expect
bugs, holes in logic and such.

Example usage:
http://github.com/dpp/liftweb/blob/f974b41c56afa500e9d93371f0ce21bde3c854ce/lift-json/src/test/scala/net/liftweb/json/XmlExamples.scala

Cheers Joni
> Beginning Scalahttp://www.apress.com/book/view/1430219890

Joni Freeman

unread,
Sep 24, 2009, 3:24:49 AM9/24/09
to Lift
Hi,

There is now improved XML support in lift master. See a short section
in README:
http://github.com/dpp/liftweb/tree/master/lift-json/

and some executable examples:
http://github.com/dpp/liftweb/blob/master/lift-json/src/test/scala/net/liftweb/json/XmlExamples.scala

Cheers Joni

On Sep 2, 9:27 pm, harryh <har...@gmail.com> wrote:
> Is there anything built into lift that will doXML-> JSON

Timothy Perrett

unread,
Sep 24, 2009, 5:04:06 AM9/24/09
to lif...@googlegroups.com
Nice stuff Joni!

Cheers, Tim

David Pollak

unread,
Sep 24, 2009, 12:18:27 PM9/24/09
to lif...@googlegroups.com


On Thu, Sep 24, 2009 at 2:04 AM, Timothy Perrett <tim...@getintheloop.eu> wrote:

Nice stuff Joni!

+1
 



--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Surf the harmonics

harryh

unread,
Sep 24, 2009, 3:58:38 PM9/24/09
to Lift
This is really really great. Will totally help me out a lot. Thanks!

-harryh

On Sep 24, 3:24 am, Joni Freeman <freeman.j...@gmail.com> wrote:
> Hi,
>
> There is now improved XML support in lift master. See a short section
> in README:http://github.com/dpp/liftweb/tree/master/lift-json/
>
> and some executable examples:http://github.com/dpp/liftweb/blob/master/lift-json/src/test/scala/ne...

Indrajit Raychaudhuri

unread,
Sep 24, 2009, 5:13:52 PM9/24/09
to lif...@googlegroups.com

On 24/09/09 2:34 PM, Timothy Perrett wrote:
>
> Nice stuff Joni!

Indeed. That this module gets lots of love shows.
Everything nice and tidy in a neat little package.

Did I mention that it's test cases serve as docs too?

/Indrajit

Timothy Perrett

unread,
Sep 25, 2009, 4:21:07 AM9/25/09
to lif...@googlegroups.com
Are there any plans to supersede the JsObj stuff with what is in lift-
json ? Seems like that would make a lot more sense, no?

Cheers, Tim

David Pollak

unread,
Sep 25, 2009, 9:07:35 AM9/25/09
to lif...@googlegroups.com
On Fri, Sep 25, 2009 at 1:21 AM, Timothy Perrett <tim...@getintheloop.eu> wrote:

Are there any plans to supersede the JsObj stuff with what is in lift-
json ? Seems like that would make a lot more sense, no?

Yes.
 

--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Git some: http://github.com/dpp

Timothy Perrett

unread,
Sep 25, 2009, 9:19:35 AM9/25/09
to lif...@googlegroups.com
Sorry Dave, but the brevity of your reply left me somewhat confused... "yes it makes sense to do that" or "yes, we'll be replacing everything with lift-json" ?

Cheers, Tim

David Pollak

unread,
Sep 25, 2009, 10:27:48 AM9/25/09
to lif...@googlegroups.com
On Fri, Sep 25, 2009 at 6:19 AM, Timothy Perrett <tim...@getintheloop.eu> wrote:
Sorry Dave, but the brevity of your reply left me somewhat confused... "yes it makes sense to do that" or "yes, we'll be replacing everything with lift-json" ?

Eventually, we'll be integrating all of lift-json into the existing Lift JavaScript infrastructure.
 
Surf the harmonics

Thomas Rampelberg

unread,
Sep 25, 2009, 11:03:10 AM9/25/09
to lif...@googlegroups.com
This is really great, making XML as easy to generate as JSON was
something I thought I'd never see.
Reply all
Reply to author
Forward
0 new messages