summaryrefslogtreecommitdiff
path: root/src/test/scala
diff options
context:
space:
mode:
authorMatthew Livesey <matt@mjlivesey.co.uk>2015-09-13 17:04:27 +0100
committerMatthew Livesey <matt@mjlivesey.co.uk>2015-09-13 17:04:27 +0100
commitdd62833aca49ee5aa6a1dbb6b6d274d44c7b52dc (patch)
treef7309a7bba38e61cf65c1cfd4d7de46ba839592f /src/test/scala
parentf185c5fb1d53f5399dbbb7229d7f7ae02971be20 (diff)
downloadspray-json-dd62833aca49ee5aa6a1dbb6b6d274d44c7b52dc.tar.gz
spray-json-dd62833aca49ee5aa6a1dbb6b6d274d44c7b52dc.tar.bz2
spray-json-dd62833aca49ee5aa6a1dbb6b6d274d44c7b52dc.zip
Added method for sorted print
This method prints in the same format as PrettyPrinter but sorts the keys of each object lexicographically. The impetus for this change was this question: http://stackoverflow.com/questions/31418626/sort-fields-in-rendered-json In general it is useful to be able to have more deterministic control over the ordering of output, if diff tools are to be used.
Diffstat (limited to 'src/test/scala')
-rw-r--r--src/test/scala/spray/json/SortedPrinterSpec.scala50
1 files changed, 50 insertions, 0 deletions
diff --git a/src/test/scala/spray/json/SortedPrinterSpec.scala b/src/test/scala/spray/json/SortedPrinterSpec.scala
new file mode 100644
index 0000000..0ea1045
--- /dev/null
+++ b/src/test/scala/spray/json/SortedPrinterSpec.scala
@@ -0,0 +1,50 @@
+package spray.json
+
+import scala.collection.immutable.ListMap
+import org.specs2.mutable._
+
+class SortedPrinterSpec extends Specification {
+
+ "The SortedPrinter" should {
+ "print a more complicated JsObject nicely aligned with fields sorted" in {
+ val obj = JsonParser {
+ """{
+ | "Unic\u00f8de" : "Long string with newline\nescape",
+ | "Boolean no": false,
+ | "number": -1.2323424E-5,
+ | "key with \"quotes\"" : "string",
+ | "key with spaces": null,
+ | "simpleKey" : "some value",
+ | "zero": 0,
+ | "sub object" : {
+ | "sub key": 26.5,
+ | "a": "b",
+ | "array": [1, 2, { "yes":1, "no":0 }, ["a", "b", null], false]
+ | },
+ | "Boolean yes":true
+ |}""".stripMargin
+ }
+ SortedPrinter(obj) mustEqual {
+ """{
+ | "Boolean no": false,
+ | "Boolean yes": true,
+ | "Unic\u00f8de": "Long string with newline\nescape",
+ | "key with \"quotes\"": "string",
+ | "key with spaces": null,
+ | "number": -0.000012323424,
+ | "simpleKey": "some value",
+ | "sub object": {
+ | "a": "b",
+ | "array": [1, 2, {
+ | "no": 0,
+ | "yes": 1
+ | }, ["a", "b", null], false],
+ | "sub key": 26.5
+ | },
+ | "zero": 0
+ |}""".stripMargin
+ }
+ }
+ }
+
+}