Permalink
Browse files

Add Map support to JSON DSL

  • Loading branch information...
Joni Freeman
Joni Freeman committed Feb 22, 2011
1 parent a780731 commit 0c24ac3d0fc07ff970d565ba97a901e2c2d5b334
@@ -440,7 +440,12 @@ trait Implicits {
*/
object JsonDSL extends JsonDSL
trait JsonDSL extends Implicits {
- implicit def seq2jvalue[A <% JValue](s: Traversable[A]) = JArray(s.toList.map { a => val v: JValue = a; v })
+ implicit def seq2jvalue[A <% JValue](s: Traversable[A]) =
+ JArray(s.toList.map { a => val v: JValue = a; v })
+
+ implicit def map2jvalue[A <% JValue](m: Map[String, A]) =
+ JObject(m.toList.map { case (k, v) => JField(k, v) })
+
implicit def option2jvalue[A <% JValue](opt: Option[A]): JValue = opt match {
case Some(x) => x
case None => JNothing
@@ -129,6 +129,13 @@ object Examples extends Specification {
compact(render(ints)) mustEqual """[35,33]"""
}
+ "Generate JSON with DSL example" in {
+ val json: JValue =
+ ("id" -> 5) ~
+ ("tags" -> Map("a" -> 5, "b" -> 7))
+ compact(render(json)) mustEqual """{"id":5,"tags":{"a":5,"b":7}}"""
+ }
+
val lotto = """
{
"lotto":{

0 comments on commit 0c24ac3

Please sign in to comment.