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/main/scala/spray/json/PrettyPrinter.scala | |
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/main/scala/spray/json/PrettyPrinter.scala')
-rw-r--r-- | src/main/scala/spray/json/PrettyPrinter.scala | 4 |
1 files changed, 3 insertions, 1 deletions
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(": ") |