summaryrefslogtreecommitdiff
path: root/test/files/run/json.scala
diff options
context:
space:
mode:
authorDerek Chen-Beker <dchenbecker@gmail.com>2010-10-08 20:22:13 +0000
committerDerek Chen-Beker <dchenbecker@gmail.com>2010-10-08 20:22:13 +0000
commit908ed2f29f4dbe03bb2ff73341a74c524f44f964 (patch)
tree6e7ba5f87f82d0d9ecfd018de561127d5376cfb2 /test/files/run/json.scala
parent4af97e33e7f60e61df1483af3605050a3af5dfb6 (diff)
downloadscala-908ed2f29f4dbe03bb2ff73341a74c524f44f964.tar.gz
scala-908ed2f29f4dbe03bb2ff73341a74c524f44f964.tar.bz2
scala-908ed2f29f4dbe03bb2ff73341a74c524f44f964.zip
Made some adjustments to toString formatting of...
Made some adjustments to toString formatting of JSON Closes #3605 Hopefully this is the last time I have to close this ticket. In addition to default behavior, the end user can specify their own JSON value formatting function if they want to customize it.
Diffstat (limited to 'test/files/run/json.scala')
-rw-r--r--test/files/run/json.scala88
1 files changed, 65 insertions, 23 deletions
diff --git a/test/files/run/json.scala b/test/files/run/json.scala
index 0a141dc38c..1e0b5be94b 100644
--- a/test/files/run/json.scala
+++ b/test/files/run/json.scala
@@ -15,65 +15,107 @@ object Test extends Application {
case x => x.toString
}
+ /*
+ * This method takes input JSON values and sorts keys on objects.
+ */
def sortJSON(in : Any) : Any = in match {
case l : List[_] => l.map(sortJSON)
case m : Map[String,_] => TreeMap(m.mapElements(sortJSON).elements.toSeq : _*)
+ // For the object versions, sort their contents, ugly casts and all...
+ case JSONObject(data) => JSONObject(sortJSON(data).asInstanceOf[Map[String,Any]])
+ case JSONArray(data) => JSONArray(sortJSON(data).asInstanceOf[List[Any]])
case x => x
}
// For this one, just parsing should be considered a pass
def printJSON(given : String) {
- JSON parseFull given match {
+ JSON parseRaw given match {
case None => println("Parse failed for \"%s\"".format(given))
- case Some(parsed) => println("Passed: " + sortJSON(parsed))
+ case Some(parsed) => println("Passed parse : " + sortJSON(parsed))
}
}
- def printJSON(given : String, expected : Any) {
- JSON parseFull given match {
+ // For this usage, do a raw parse (to JSONObject/JSONArray)
+ def printJSON(given : String, expected : JSONType) {
+ printJSON(given, JSON.parseRaw, expected)
+ }
+
+ // For this usage, do a raw parse (to JSONType and subclasses)
+ def printJSONFull(given : String, expected : Any) {
+ printJSON(given, JSON.parseFull, expected)
+ }
+
+ // For this usage, do configurable parsing so that you can do raw if desired
+ def printJSON[T](given : String, parser : String => T, expected : Any) {
+ parser(given) match {
case None => println("Parse failed for \"%s\"".format(given))
case Some(parsed) => if (parsed == expected) {
- println("Passed: " + parsed)
+ println("Passed compare: " + parsed)
} else {
val eStr = sortJSON(expected).toString
val pStr = sortJSON(parsed).toString
+ stringDiff(eStr,pStr)
+ }
+ }
+ }
- // Figure out where the Strings differ and generate a marker
- val mismatchPosition = eStr.toList.zip(pStr.toList).findIndexOf({case (a,b) => a != b}) match {
- case -1 => Math.min(eStr.length, pStr.length)
+ def stringDiff (expected : String, actual : String) {
+ if (expected != actual) {
+ // Figure out where the Strings differ and generate a marker
+ val mismatchPosition = expected.toList.zip(actual.toList).findIndexOf({case (x,y) => x != y}) match {
+ case -1 => Math.min(expected.length, actual.length)
case x => x
}
val reason = (" " * mismatchPosition) + "^"
- println("Expected, got:\n %s\n %s (from \"%s\")\n %s".format(eStr, pStr, given, reason))
- }
+ println("Expected: %s\nGot : %s \n %s".format(expected, actual, reason))
+
+ } else {
+ println("Passed compare: " + actual)
}
}
+
// The library should differentiate between lower case "l" and number "1" (ticket #136)
- printJSON("{\"name\": \"value\"}", Map("name" -> "value"))
- printJSON("{\"name\": \"va1ue\"}", Map("name" -> "va1ue"))
- printJSON("{\"name\": { \"name1\": \"va1ue1\", \"name2\": \"va1ue2\" } }",
- Map("name" -> Map("name1" -> "va1ue1", "name2" -> "va1ue2")))
+ printJSON("{\"name\" : \"value\"}", JSONObject(Map("name" -> "value")))
+ printJSON("{\"name\" : \"va1ue\"}", JSONObject(Map("name" -> "va1ue")))
+ printJSON("{\"name\" : { \"name1\" : \"va1ue1\", \"name2\" : \"va1ue2\" } }",
+ JSONObject(Map("name" -> JSONObject(Map("name1" -> "va1ue1", "name2" -> "va1ue2")))))
// Unicode escapes should be handled properly
- printJSON("{\"name\": \"\\u0022\"}")
+ printJSON("{\"name\" : \"\\u0022\"}")
// The library should return a map for JSON objects (ticket #873)
- printJSON("""{"function":"add_symbol"}""", Map("function" -> "add_symbol"))
+ printJSONFull("{\"function\" : \"add_symbol\"}", Map("function" -> "add_symbol"))
// The library should recurse into arrays to find objects (ticket #2207)
- printJSON("""[{"a": "team"},{"b": 52}]""", List(Map("a" -> "team"), Map("b" -> 52.0)))
+ printJSON("[{\"a\" : \"team\"},{\"b\" : 52}]", JSONArray(List(JSONObject(Map("a" -> "team")), JSONObject(Map("b" -> 52.0)))))
// The library should differentiate between empty maps and lists (ticket #3284)
- printJSON("{}", Map())
- printJSON("[]", List())
+ printJSONFull("{}", Map())
+ printJSONFull("[]", List())
// Lists should be returned in the same order as specified
- printJSON("[4,1,3,2,6,5,8,7]", List[Double](4,1,3,2,6,5,8,7))
+ printJSON("[4,1,3,2,6,5,8,7]", JSONArray(List[Double](4,1,3,2,6,5,8,7)))
// Additional tests
printJSON("{\"age\": 0}")
+ // The library should do a proper toString representation using default and custom renderers (ticket #3605)
+ stringDiff("{\"name\" : \"va1ue\"}", JSONObject(Map("name" -> "va1ue")).toString)
+ stringDiff("{\"name\" : {\"name1\" : \"va1ue1\", \"name2\" : \"va1ue2\"}}",
+ JSONObject(Map("name" -> JSONObject(TreeMap("name1" -> "va1ue1", "name2" -> "va1ue2")))).toString)
+
+ stringDiff("[4.0, 1.0, 3.0, 2.0, 6.0, 5.0, 8.0, 7.0]", JSONArray(List[Double](4,1,3,2,6,5,8,7)).toString)
+
+ // A test method that escapes all characters in strings
+ def escapeEverything (in : Any) : String = in match {
+ case s : String => "\"" + s.map(c => "\\u%04x".format(c : Int)).mkString + "\""
+ case jo : JSONObject => jo.toString(escapeEverything)
+ case ja : JSONArray => ja.toString(escapeEverything)
+ case other => other.toString
+ }
+
+ stringDiff("{\"\\u006e\\u0061\\u006d\\u0065\" : \"\\u0076\\u0061\\u006c\"}", JSONObject(Map("name" -> "val")).toString(escapeEverything))
println
@@ -111,7 +153,7 @@ object Test extends Application {
)
- printJSON(sample1, sample1Obj)
+ printJSONFull(sample1, sample1Obj)
println
// from http://www.developer.com/lang/jscript/article.php/3596836
@@ -139,7 +181,7 @@ object Test extends Application {
{"type": "home", "value": "http://seankelly.tv/"}
]
}"""
- //println(sample2)
+
printJSON(sample2)
println
@@ -235,7 +277,7 @@ object Test extends Application {
"taglib-location": "/WEB-INF/tlds/cofax.tld"}
}
}"""
- //println(sample3)
+
printJSON(sample3)
println
}