lift json serializing mixed type array

122 views
Skip to first unread message

Sagar Mehta

unread,
Jul 16, 2012, 7:05:25 PM7/16/12
to Lift
I have the following json structure which I'd like to parse and
extract using a case class.

{
"field1": true,
"field2": "hi",
"entries": [
[
"/blah",
{
"fielda": 70615911,
"fieldb": "4358367001dfbd6",
}
],
[
"/hello.txt",
{
"fielda": 363696459,
"fieldb": "15ad914b001dfbd6",
}
]
]
}

The thing I'm having trouble with is the inner array in entries which
is of a mixed type (a JString and JObject if I just print out the
parsed representation). I've tried to just use generic Any without
luck and specifying the inner object as either a JObject or JString
type obviously also fails. How can I set it up to extract the inner
array in this example?

case class JsonObj(field1: Boolean, field2: String, entries:
List[List[Any]])



Exception in thread "main" net.liftweb.json.MappingException: No
usable value for entries
No information known about type
at net.liftweb.json.Meta$.fail(Meta.scala:190)
at net.liftweb.json.Extraction$.mkValue$1(Extraction.scala:356)
at net.liftweb.json.Extraction$.build$1(Extraction.scala:316)
at net.liftweb.json.Extraction$$anonfun$12.apply(Extraction.scala:
252)
at net.liftweb.json.Extraction$$anonfun$12.apply(Extraction.scala:
252)
at scala.collection.TraversableLike$$anonfun$map
$1.apply(TraversableLike.scala:194)
at scala.collection.TraversableLike$$anonfun$map
$1.apply(TraversableLike.scala:194)
at scala.collection.LinearSeqOptimized
$class.foreach(LinearSeqOptimized.scala:59)
at scala.collection.immutable.List.foreach(List.scala:45)
at scala.collection.TraversableLike$class.map(TraversableLike.scala:
194)
at scala.collection.immutable.List.map(List.scala:45)
at net.liftweb.json.Extraction$.instantiate$1(Extraction.scala:252)
at net.liftweb.json.Extraction$.newInstance$1(Extraction.scala:285)
at net.liftweb.json.Extraction$.build$1(Extraction.scala:314)
at net.liftweb.json.Extraction$.net$liftweb$json$Extraction$
$extract0(Extraction.scala:365)
at net.liftweb.json.Extraction$.net$liftweb$json$Extraction$
$extract0(Extraction.scala:198)
at net.liftweb.json.Extraction$.extract(Extraction.scala:42)
at net.liftweb.json.JsonAST$JValue.extract(JsonAST.scala:300)
at com.magicnotebook.index.api.DropboxOAuth$.main(DropboxOAuth.scala:
23)
at com.magicnotebook.index.api.DropboxOAuth.main(DropboxOAuth.scala)

Diego Medina

unread,
Jul 18, 2012, 8:43:02 PM7/18/12
to lif...@googlegroups.com
I tried several ways and I could not get to serialize the /blah and
/hello.txt values.

I even used Joni's secret weapon [1] and the closest I got was:

case class Root(field1: Boolean, field2: String, entries: List[Entrie])
case class Entrie(fielda: Int, fieldb: String)

which returns:

Root(true,hi,List(Entrie(70615911,4358367001dfbd6),
Entrie(363696459,15ad914b001dfbd6)))

(omitting the first String value)



Are you generating the json? or maybe you could use the transform
method to convert the json into a different structure [2]
[1] http://pastie.org/2964249
[2] https://github.com/lift/framework/tree/master/core/json

Regards,

Diego
> --
> 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



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

Diego Medina

unread,
Jul 18, 2012, 9:46:49 PM7/18/12
to lif...@googlegroups.com
this works
https://gist.github.com/3140243

see if it fits your needs.

Diego

IL

unread,
Jul 19, 2012, 1:57:47 AM7/19/12
to lif...@googlegroups.com

Sagar Mehta

unread,
Jul 19, 2012, 12:17:26 PM7/19/12
to Lift
Great got it to work. I went with IL's approach since it seemed a
little more concise. Thanks.
Reply all
Reply to author
Forward
0 new messages