Created
June 21, 2018 16:33
-
-
Save dedels/6922eb7751e6b3742ded281bb3ece272 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import spray.json._ | |
import scala.collection.immutable.{ListMap} | |
import scala.io.Source | |
trait SortedPrettyPrinter extends PrettyPrinter { | |
override protected def printObject(members: Map[String, JsValue], sb: java.lang.StringBuilder, indent: Int) { | |
sb.append("{\n") | |
printSeq(ListMap(members.toSeq.sortBy(_._1):_*), sb.append(",\n")) { m => | |
this.printIndent(sb, indent + Indent) | |
printString(m._1, sb) | |
sb.append(": ") | |
print(m._2, sb, indent + Indent) | |
} | |
sb.append('\n') | |
printIndent(sb, indent) | |
sb.append("}") | |
} | |
} | |
object SortedPrettyPrinter extends SortedPrettyPrinter | |
//use like: | |
val contents = Map("hello", 123) | |
val output:String = SortedPrettyPrinter(contents.toJson) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment