From dd62833aca49ee5aa6a1dbb6b6d274d44c7b52dc Mon Sep 17 00:00:00 2001 From: Matthew Livesey Date: Sun, 13 Sep 2015 17:04:27 +0100 Subject: 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. --- src/test/scala/spray/json/SortedPrinterSpec.scala | 50 +++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/test/scala/spray/json/SortedPrinterSpec.scala (limited to 'src/test/scala') 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 + } + } + } + +} -- cgit v1.2.3