Generate case class from parsed json

49 views
Skip to first unread message

Diego Medina

unread,
Dec 2, 2011, 9:55:10 AM12/2/11
to Lift
Hi,

Most of the time what I use lift json I start by starting the repl,
assigning the json I want to work with to a value and then look at the
parsed result to construct a case class or a group of them. Is there a
way to have lift json generate the case classes for me? even if it
would end up copying and pasting it on my code.

Sample session:

scala> val json= """
| {"results":[
| {"seq":31,"id":"1","changes":[{"rev":"30-228b36767c332568d40a71438b672877"}]},
| {"seq":32,"id":"2","changes":[{"rev":"2-92a882a939a8150f6770bb2eaace2301"}]}
| ],
| "last_seq":32}
| """
json: java.lang.String =
"
{"results":[
{"seq":31,"id":"1","changes":[{"rev":"30-228b36767c332568d40a71438b672877"}]},
{"seq":32,"id":"2","changes":[{"rev":"2-92a882a939a8150f6770bb2eaace2301"}]}
],
"last_seq":32}
"

scala> val p= parse(json)
p: net.liftweb.json.package.JValue =
JObject(List(JField(results,JArray(List(JObject(List(JField(seq,JInt(31)),
JField(id,JString(1)),
JField(changes,JArray(List(JObject(List(JField(rev,JString(30-228b36767c332568d40a71438b672877))))))))),
JObject(List(JField(seq,JInt(32)), JField(id,JString(2)),
JField(changes,JArray(List(JObject(List(JField(rev,JString(2-92a882a939a8150f6770bb2eaace2301)))))))))))),
JField(last_seq,JInt(32))))

scala>

and now I look at the result of p and start writing my case class/es

Thanks

Diego

--
Diego Medina
Web Developer
di...@fmpwizard.com
http://www.fmpwizard.com

Joni Freeman

unread,
Dec 4, 2011, 8:24:39 AM12/4/11
to lif...@googlegroups.com
Hi,

There's no function to do that but it is a quite interesting idea. I quickly hacked
a prototype function which you can adjust to suit your needs:

http://pastie.org/2964249

Cheers Joni

Diego Medina

unread,
Dec 4, 2011, 8:53:48 AM12/4/11
to lif...@googlegroups.com

Thanks Joni!

Diego
Sent from my android cell

--
Lift, the simply functional web framework: http://liftweb.net
Code: http://github.com/lift
Discussion: http://groups.google.com/group/liftweb
Stuck? Help us help you: https://www.assembla.com/wiki/show/liftweb/Posting_example_code

Peter Robinett

unread,
Dec 4, 2011, 12:47:52 PM12/4/11
to lif...@googlegroups.com
Cool, like scalaxb but for JSON! Just think you're half way to a library once you parse the JSON examples in an APIs documentation. =)

Peter

Joni Freeman

unread,
Dec 4, 2011, 1:02:30 PM12/4/11
to lif...@googlegroups.com
Indeed :)

Cheers Joni
Reply all
Reply to author
Forward
0 new messages