diff options
author | Matthew Livesey <matt@mjlivesey.co.uk> | 2015-09-13 17:04:27 +0100 |
---|---|---|
committer | Matthew Livesey <matt@mjlivesey.co.uk> | 2015-09-13 17:04:27 +0100 |
commit | dd62833aca49ee5aa6a1dbb6b6d274d44c7b52dc (patch) | |
tree | f7309a7bba38e61cf65c1cfd4d7de46ba839592f /src | |
parent | f185c5fb1d53f5399dbbb7229d7f7ae02971be20 (diff) | |
download | spray-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')
-rw-r--r-- | src/main/scala/spray/json/JsValue.scala | 1 | ||||
-rw-r--r-- | src/main/scala/spray/json/PrettyPrinter.scala | 4 | ||||
-rw-r--r-- | src/main/scala/spray/json/SortedPrinter.scala | 9 | ||||
-rw-r--r-- | src/test/scala/spray/json/SortedPrinterSpec.scala | 50 |
4 files changed, 63 insertions, 1 deletions
diff --git a/src/main/scala/spray/json/JsValue.scala b/src/main/scala/spray/json/JsValue.scala index b21672d..a269475 100644 --- a/src/main/scala/spray/json/JsValue.scala +++ b/src/main/scala/spray/json/JsValue.scala @@ -28,6 +28,7 @@ sealed abstract class JsValue { def toString(printer: (JsValue => String)) = printer(this) def compactPrint = CompactPrinter(this) def prettyPrint = PrettyPrinter(this) + def sortedPrint = SortedPrinter(this) def convertTo[T :JsonReader]: T = jsonReader[T].read(this) /** diff --git a/src/main/scala/spray/json/PrettyPrinter.scala b/src/main/scala/spray/json/PrettyPrinter.scala index 57cf35e..6af5443 100644 --- a/src/main/scala/spray/json/PrettyPrinter.scala +++ b/src/main/scala/spray/json/PrettyPrinter.scala @@ -37,9 +37,11 @@ trait PrettyPrinter extends JsonPrinter { } } + protected def organiseMembers(members: Map[String, JsValue]): Seq[(String, JsValue)] = members.toSeq + protected def printObject(members: Map[String, JsValue], sb: StringBuilder, indent: Int) { sb.append("{\n") - printSeq(members, sb.append(",\n")) { m => + printSeq(organiseMembers(members), sb.append(",\n")) { m => printIndent(sb, indent + Indent) printString(m._1, sb) sb.append(": ") diff --git a/src/main/scala/spray/json/SortedPrinter.scala b/src/main/scala/spray/json/SortedPrinter.scala new file mode 100644 index 0000000..070d36c --- /dev/null +++ b/src/main/scala/spray/json/SortedPrinter.scala @@ -0,0 +1,9 @@ +package spray.json + +trait SortedPrinter extends PrettyPrinter { + + override protected def organiseMembers(members: Map[String, JsValue]): Seq[(String, JsValue)] = + members.toSeq.sortBy(_._1) +} + +object SortedPrinter extends SortedPrinter 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 + } + } + } + +} |