Skip to content

Instantly share code, notes, and snippets.

@dedels
Created June 21, 2018 16:33
Show Gist options
  • Save dedels/6922eb7751e6b3742ded281bb3ece272 to your computer and use it in GitHub Desktop.
Save dedels/6922eb7751e6b3742ded281bb3ece272 to your computer and use it in GitHub Desktop.
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