home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Lift Framework example source code file (XmlBugs.scala)

This example Lift Framework source code file (XmlBugs.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Lift Framework tags/keywords

bar, diff, foo, harryh's, harryh's, jnothing, jnothing, jono's, json, nearby, nearby, specification, xml, xml

The Lift Framework XmlBugs.scala source code

 * Copyright 2009-2011 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
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 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

object XmlBugs extends Specification {
  import Xml._
  import scala.xml.{Group, Text}

  "HarryH's XML parses correctly" in {
    val xml1 = <venue>123
    val xml2 = <venue> {"1"}{"23"} 
    Xml.toJson(xml1) must_== Xml.toJson(xml2)

  "HarryH's XML with attributes parses correctly" in {
    val json = toJson(<tips>10)
    Printer.compact(render(json)) mustEqual """{"tips":{"group":{"type":"Nearby","tip":{"id":"10"}}}}"""

  "Jono's XML with attributes parses correctly" in {
    val example1 = <word term="example" self="http://localhost:8080/word/example" available="true">content
    val expected1 = """{"word":"content","self":"http://localhost:8080/word/example","term":"example","available":"true"}"""

    val example2 = <word term="example" self="http://localhost:8080/word/example" available="true">
    val expected2 = """{"self":"http://localhost:8080/word/example","term":"example","available":"true"}"""

    (toJson(example1) diff parse(expected1)) mustEqual Diff(JNothing, JNothing, JNothing)
    (toJson(example2) diff parse(expected2)) mustEqual Diff(JNothing, JNothing, JNothing)

  "Nodes with attributes converted to correct JSON" in {
    val xml =
        <n id="10" x="abc" />
        <n id="11" x="bcd" />
    val expected = """{"root":{"n":[{"x":"abc","id":"10"},{"x":"bcd","id":"11"}]}}"""
    Printer.compact(render(toJson(xml))) mustEqual expected

  "XML with empty node is converted correctly to JSON" in {
    val xml =
    val expected = """{"tips":{"group":[{"type":"Foo"},{"type":"Bar","tip":[{"text":"xxx"},{"text":"yyy"}]}]}}"""
    Printer.compact(render(toJson(xml))) mustEqual expected

Other Lift Framework examples (source code examples)

Here is a short list of links related to this Lift Framework XmlBugs.scala source code file:

my book on functional programming


new blog posts


Copyright 1998-2019 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.