lift-Json doesn't appear to be correctly handling attributes
When converting XML to Json attributes are being lost, given the below XML document, we would expect
{"word":{"word":"content","self":"http://localhost:8080/word/example","term":"example","available":"true"}}
where as we get
{"word":"content"}
example XML:
<word term="example" self="http://localhost:8080/word/example" available="true">content</word>
This is been tested on M1 and trunk.
Link to the google group thread :
http://groups.google.com/group/liftweb/browse_thread/thread/4d056e8e5c82671e#
To repeat
// From the command line, adjust paths as needed
$scala -cp
/Users/jono/.m2/repository/com/thoughtworks/paranamer/paranamer/2.0/paranamer-2.0.jar:/Users/jono/.m2/repository/net/liftweb/lift-json/2.0-SNAPSHOT/lift-json-2.0-SNAPSHOT.jar
import scala.xml._
import net.liftweb.json.JsonAST._
import net.liftweb.json.Printer
import net.liftweb.json.Xml
val example1 = <word term="example" self="http://localhost:8080/word/example" available="true">content</word>
val expected1 = """{"word":{"word":"content","self":"http://localhost:8080/word/example","term":"example","available":"true"}}"""
val got1 = Printer.compact(render(Xml.toJson(example1)))
val example2 = <word term="example" self="http://localhost:8080/word/example" available="true"></word>
val expected2 = """{"word":{"word":"content","self":"http://localhost:8080/word/example","term":"example","available":"true"}}"""
val got2 = Printer.compact(render(Xml.toJson(example2)))
// Add to src/test/scala/net/liftweb/json/XmlBugs.scala
"Jono's XML with attributes parses correctly" in {
val example1 = <word term="example" self="http://localhost:8080/word/example" available="true">content</word>
val expected1 = """{"word":{"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"></word>
val expected2 = """{"word":{"word":"content","self":"http://localhost:8080/word/example","term":"example","available":"true"}}"""
Printer.compact(render(toJson(example1))) mustEqual expected1
Printer.compact(render(toJson(example2))) mustEqual expected2
}
Here is what I get when I run the example provided.
$scala -cp /Users/jono/.m2/repository/com/thoughtworks/paranamer/paranamer/2.0/paranamer-2.0.jar:/Users/jono/.m2/repository/net/liftweb/lift-json/2.0-SNAPSHOT/lift-json-2.0-SNAPSHOT.jar
Welcome to Scala version 2.7.7.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_17).
Type in expressions to have them evaluated.
Type :help for more information.
scala> import scala.xml._
import scala.xml._
scala> import net.liftweb.json.JsonAST._
import net.liftweb.json.JsonAST._
scala> import net.liftweb.json.Printer
import net.liftweb.json.Printer
scala> import net.liftweb.json.Xml
import net.liftweb.json.Xml
scala>
scala> val example1 = <word term="example" self="http://localhost:8080/word/example" available="true">content</word>
example1: scala.xml.Elem = <word self="http://localhost:8080/word/example" term="example" available="true">content</word>
scala> val expected1 = """{"word":{"word":"content","self":"http://localhost:8080/word/example","term":"example","available":"true"}}"""
expected1: java.lang.String = {"word":{"word":"content","self":"http://localhost:8080/word/example","term":"example","available":"true"}}
scala> val got1 = Printer.compact(render(Xml.toJson(example1)))
got1: String = {"word":"content"}
scala>
{"word":{"word":"content","self":"http://localhost:8080/word/example","term":"example","available":"true"}}
where as we get
{"word":"content"}
example XML:
<word term="example" self="http://localhost:8080/word/example" available="true">content</word>
This is been tested on M1 and trunk.
Link to the google group thread :
http://groups.google.com/group/liftweb/browse_thread/thread/4d056e8e5c82671e#
To repeat
// From the command line, adjust paths as needed
$scala -cp
/Users/jono/.m2/repository/com/thoughtworks/paranamer/paranamer/2.0/paranamer-2.0.jar:/Users/jono/.m2/repository/net/liftweb/lift-json/2.0-SNAPSHOT/lift-json-2.0-SNAPSHOT.jar
import scala.xml._
import net.liftweb.json.JsonAST._
import net.liftweb.json.Printer
import net.liftweb.json.Xml
val example1 = <word term="example" self="http://localhost:8080/word/example" available="true">content</word>
val expected1 = """{"word":{"word":"content","self":"http://localhost:8080/word/example","term":"example","available":"true"}}"""
val got1 = Printer.compact(render(Xml.toJson(example1)))
val example2 = <word term="example" self="http://localhost:8080/word/example" available="true"></word>
val expected2 = """{"word":{"word":"content","self":"http://localhost:8080/word/example","term":"example","available":"true"}}"""
val got2 = Printer.compact(render(Xml.toJson(example2)))
// Add to src/test/scala/net/liftweb/json/XmlBugs.scala
"Jono's XML with attributes parses correctly" in {
val example1 = <word term="example" self="http://localhost:8080/word/example" available="true">content</word>
val expected1 = """{"word":{"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"></word>
val expected2 = """{"word":{"word":"content","self":"http://localhost:8080/word/example","term":"example","available":"true"}}"""
Printer.compact(render(toJson(example1))) mustEqual expected1
Printer.compact(render(toJson(example2))) mustEqual expected2
}
Here is what I get when I run the example provided.
$scala -cp /Users/jono/.m2/repository/com/thoughtworks/paranamer/paranamer/2.0/paranamer-2.0.jar:/Users/jono/.m2/repository/net/liftweb/lift-json/2.0-SNAPSHOT/lift-json-2.0-SNAPSHOT.jar
Welcome to Scala version 2.7.7.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_17).
Type in expressions to have them evaluated.
Type :help for more information.
scala> import scala.xml._
import scala.xml._
scala> import net.liftweb.json.JsonAST._
import net.liftweb.json.JsonAST._
scala> import net.liftweb.json.Printer
import net.liftweb.json.Printer
scala> import net.liftweb.json.Xml
import net.liftweb.json.Xml
scala>
scala> val example1 = <word term="example" self="http://localhost:8080/word/example" available="true">content</word>
example1: scala.xml.Elem = <word self="http://localhost:8080/word/example" term="example" available="true">content</word>
scala> val expected1 = """{"word":{"word":"content","self":"http://localhost:8080/word/example","term":"example","available":"true"}}"""
expected1: java.lang.String = {"word":{"word":"content","self":"http://localhost:8080/word/example","term":"example","available":"true"}}
scala> val got1 = Printer.compact(render(Xml.toJson(example1)))
got1: String = {"word":"content"}
scala>
Leave a comment
on 2010-02-18 21:08 *
By Peter Robinett
(from github) See also http://groups.google.com/group/liftweb/browse_thread/thread/a25a93f55c181475