net.liftweb.json.JsonDSL and reduceLeft(_ ~ _)

77 views
Skip to first unread message

Andy C

unread,
May 21, 2013, 5:00:06 PM5/21/13
to lif...@googlegroups.com
Hi,

I would like to reduce a list of tuples into a JSON piece. This is an extract from my context:

import net.liftweb.json._
import net.liftweb.json.JsonDSL._

scala> val tuples=List( "aaa"->List(1,2,3) , "bbb"->List(10,20,30), "ccc"->List(100,200,300))
tuples: List[(String, List[Int])] = List((aaa,List(1, 2, 3)), (bbb,List(10, 20, 30)), (ccc,List(100, 200, 300)))

scala> tuples(0) ~ tuples(1) ~ tuples(2)
res8: net.liftweb.json.JsonAST.JObject = JObject(List(JField(aaa,JArray(List(JInt(1), JInt(2), JInt(3)))), JField(bbb,JArray(List(JInt(10), JInt(20), JInt(30)))), JField(ccc,JArray(List(JInt(100), JInt(200), JInt(300))))))

scala> tuples.reduceLeft(_ ~ _)
<console>:30: error: type mismatch;
 found   : net.liftweb.json.JsonAST.JObject
 required: (String, List[Int])
              tuples.reduceLeft(_ ~ _)

Apparently there is some kind of conflict here. Is there a better way of achieving the goal here of  having a simple and single line of the code to combine all JSON pieces together?

Thx in advance,
Andy

Tim Nelson

unread,
May 21, 2013, 5:38:54 PM5/21/13
to lif...@googlegroups.com
Hi,

reduceLeft requires the types to be consistent. Since you're changing types to JObject, it doesn't work here. However, foldLeft does:

tuples.foldLeft(JObject(Nil))(_ ~ _)

Tim

Andy C

unread,
May 21, 2013, 8:07:03 PM5/21/13
to lif...@googlegroups.com

Hi Tim,

Thanks a lot for the hint. Does it mean that equation is correct

JObject(Nil) ~ something  == JObject(something) ?

I actually tried that:

JObject(List(1,2,3))
<console>:35: error: type mismatch;
 found   : Int(1)
 required: net.liftweb.json.JsonAST.JField
              JObject(List(1,2,3))



I wonder if there is an method/operator to turn single objects into JObject?

Best,
Andy



Matt Farmer

unread,
May 21, 2013, 8:54:21 PM5/21/13
to lif...@googlegroups.com
Replies inline.


On Tuesday, May 21, 2013 8:07:03 PM UTC-4, AndyC wrote:

Hi Tim,

Thanks a lot for the hint. Does it mean that equation is correct

JObject(Nil) ~ something  == JObject(something) ?

Erhm. Not quite.

JObject(Nil) ~ someJField => JObject

So,

JObject(Nil) ~ ("things" -> List(1,2,3))

is valid...

JObject(Nil) ~ List(1,2,3)

is not because it wouldn't yield a proper JObject. It would be the equivalent of trying to write...

{ [1, 2, 3] }
 

I actually tried that:

JObject(List(1,2,3))
<console>:35: error: type mismatch;
 found   : Int(1)
 required: net.liftweb.json.JsonAST.JField
              JObject(List(1,2,3))


This didn't work much for the same reason I mentioned above. You can however drop wrap that list in a list of JFields and have it work.

JObject(List(JField("things", List(1,2,3))))

... I think that's right. The JsonDSL equivalent is...

("things" -> List(1,2,3))

But you can't just wrap arbitrary objects inside a JObject because it wouldn't produce valid JSON.

Hope this helps! :)
Reply all
Reply to author
Forward
0 new messages