Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

190 lines (165 sloc) 6.389 kb
* Copyright 2009-2010 WorldWide Conferencing, LLC
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* See the License for the specific language governing permissions and
* limitations under the License.
package net.liftweb
package json
import org.specs.Specification
import org.specs.runner.{Runner, JUnit}
class XmlExamplesTest extends Runner(XmlExamples) with JUnit
object XmlExamples extends Specification {
import JsonDSL._
import Xml._
import scala.xml.{Group, Text}
"Basic conversion example" in {
val json = toJson(users1)
compact(render(json)) mustEqual """{"users":{"count":"2","user":[{"disabled":"true","id":"1","name":"Harry"},{"id":"2","name":"David","nickname":"Dave"}]}}"""
"Conversion transformation example 1" in {
val json = toJson(users1).transform {
case JField("id", JString(s)) => JField("id", JInt(s.toInt))
compact(render(json)) mustEqual """{"users":{"count":"2","user":[{"disabled":"true","id":1,"name":"Harry"},{"id":2,"name":"David","nickname":"Dave"}]}}"""
"Conversion transformation example 2" in {
val json = toJson(users2).transform {
case JField("id", JString(s)) => JField("id", JInt(s.toInt))
case JField("user", x: JObject) => JField("user", JArray(x :: Nil))
compact(render(json)) mustEqual """{"users":{"user":[{"id":1,"name":"Harry"}]}}"""
"Primitive array example" in {
val xml = <chars><char>a</char><char>b</char><char>c</char></chars>
compact(render(toJson(xml))) mustEqual """{"chars":{"char":["a","b","c"]}}"""
"Lotto example which flattens number arrays into encoded string arrays" in {
def flattenArray(nums: List[JValue]) = JString(","))
val printer = new scala.xml.PrettyPrinter(100,2)
val lotto: JObject = LottoExample.json
val xml = toXml(lotto.transform {
case JField("winning-numbers", JArray(nums)) => JField("winning-numbers", flattenArray(nums))
case JField("numbers", JArray(nums)) => JField("numbers", flattenArray(nums))
printer.format(xml(0)) mustEqual printer.format(
"Band example with namespaces" in {
val json = toJson(band)
json mustEqual parse("""{
"name":"The Fall",
"song":["Hit the north","Victoria"]
"name":"mid 80s",
"song":["Eat your self fitter","My new house"]
val band =
<name>The Fall</name>
<playlist name="hits">
<song>Hit the north</song>
<playlist name="mid 80s">
<song>Eat your self fitter</song>
<song>My new house</song>
"Grouped text example" in {
val json = toJson(groupedText)
compact(render(json)) mustEqual """{"g":{"group":"foobar","url":""}}"""
val users1 =
<users count="2">
<user disabled="true">
<name nickname="Dave">David</name>
val users2 =
val url = "test"
val groupedText =
<group>{ Group(List(Text("foo"), Text("bar"))) }</group>
<url>{ url }</url>
// Examples by Jonathan Ferguson. See
// This example shows how to use a transformation function to correct JSON generated by
// default conversion rules. The transformation function 'attrToObject' makes following conversion:
// { ..., "fieldName": "", "attrName":"someValue", ...} ->
// { ..., "fieldName": { "attrName": f("someValue") }, ... }
def attrToObject(fieldName: String, attrName: String, f: JString => JValue)(json: JValue) = json.transform {
case JField(n, v: JString) if n == attrName => JObject(JField(n, f(v)) :: Nil)
case JField(n, JString("")) if n == fieldName => JNothing
} transform {
case JField(n, x: JObject) if n == attrName => JField(fieldName, x)
"Example with multiple attributes, multiple nested elements " in {
val a1 = attrToObject("stats", "count", s => JInt(s.s.toInt)) _
val a2 = attrToObject("messages", "href", identity) _
val json = a1(a2(toJson(messageXml1)))
compact(render(json)) mustEqual expected1
"Example with one attribute, one nested element " in {
val a = attrToObject("stats", "count", s => JInt(s.s.toInt)) _
compact(render(a(toJson(messageXml2)))) mustEqual expected2
compact(render(a(toJson(messageXml3)))) mustEqual expected2
val messageXml1 =
<message expiry_date="20091126" text="text" word="ant" self="me">
<stats count="0"></stats>
<messages href=""></messages>
val expected1 = """{"message":{"expiry_date":"20091126","word":"ant","text":"text","self":"me","stats":{"count":0},"messages":{"href":""}}}"""
val messageXml2 =
<message expiry_date="20091126">
<stats count="0"></stats>
val messageXml3 = <message expiry_date="20091126"><stats count="0"></stats></message>
val expected2 = """{"message":{"expiry_date":"20091126","stats":{"count":0}}}"""
Jump to Line
Something went wrong with that request. Please try again.