diff options
author | Mathias <mathias@spray.io> | 2014-09-19 15:25:35 +0200 |
---|---|---|
committer | Mathias <mathias@spray.io> | 2014-09-19 15:25:35 +0200 |
commit | f33e846b95d8e9b20a8aeb4e2ca74fd2f34206fc (patch) | |
tree | b32dab4c7a6b5aa02a8de6aa119a68a3d57c90f1 /src | |
parent | 37af0d4ca639ccea44efcb22d10a1e07abc4c04b (diff) | |
download | spray-json-f33e846b95d8e9b20a8aeb4e2ca74fd2f34206fc.tar.gz spray-json-f33e846b95d8e9b20a8aeb4e2ca74fd2f34206fc.tar.bz2 spray-json-f33e846b95d8e9b20a8aeb4e2ca74fd2f34206fc.zip |
Improve JsonPrinter to enable printing to custom StringBuilder
Diffstat (limited to 'src')
-rw-r--r-- | src/main/scala/spray/json/JsonPrinter.scala | 20 |
1 files changed, 9 insertions, 11 deletions
diff --git a/src/main/scala/spray/json/JsonPrinter.scala b/src/main/scala/spray/json/JsonPrinter.scala index d255e03..258fc5a 100644 --- a/src/main/scala/spray/json/JsonPrinter.scala +++ b/src/main/scala/spray/json/JsonPrinter.scala @@ -17,7 +17,7 @@ package spray.json import annotation.tailrec -import java.lang.StringBuilder +import java.lang.{StringBuilder => JStringBuilder} /** * A JsonPrinter serializes a JSON AST to a String. @@ -26,24 +26,22 @@ trait JsonPrinter extends (JsValue => String) { def apply(x: JsValue): String = apply(x, None) - def apply(x: JsValue, jsonpCallback: String): String = apply(x, Some(jsonpCallback)) - - def apply(x: JsValue, jsonpCallback: Option[String]): String = { - val sb = new StringBuilder + def apply(x: JsValue, + jsonpCallback: Option[String] = None, + sb: JStringBuilder = new JStringBuilder(256)): String = { jsonpCallback match { - case Some(callback) => { + case Some(callback) => sb.append(callback).append('(') print(x, sb) - sb.append(')'); - } + sb.append(')') case None => print(x, sb) } sb.toString } - def print(x: JsValue, sb: StringBuilder) + def print(x: JsValue, sb: JStringBuilder) - protected def printLeaf(x: JsValue, sb: StringBuilder) { + protected def printLeaf(x: JsValue, sb: JStringBuilder) { x match { case JsNull => sb.append("null") case JsTrue => sb.append("true") @@ -54,7 +52,7 @@ trait JsonPrinter extends (JsValue => String) { } } - protected def printString(s: String, sb: StringBuilder) { + protected def printString(s: String, sb: JStringBuilder) { import JsonPrinter._ @tailrec def firstToBeEncoded(ix: Int = 0): Int = if (ix == s.length) -1 else if (requiresEncoding(s.charAt(ix))) ix else firstToBeEncoded(ix + 1) |