summaryrefslogtreecommitdiff
path: root/src/main/scala/cc/spray/json/JsonPrinter.scala
diff options
context:
space:
mode:
authorMathias <mathias@spray.cc>2011-05-06 11:11:37 +0200
committerMathias <mathias@spray.cc>2011-05-06 11:11:37 +0200
commit0ce9cf8fce1dc475f3bb2a517e0a7698c9e0a5d9 (patch)
tree62de608642a30ec478411d27e94179ec64c5bd17 /src/main/scala/cc/spray/json/JsonPrinter.scala
downloadspray-json-0ce9cf8fce1dc475f3bb2a517e0a7698c9e0a5d9.tar.gz
spray-json-0ce9cf8fce1dc475f3bb2a517e0a7698c9e0a5d9.tar.bz2
spray-json-0ce9cf8fce1dc475f3bb2a517e0a7698c9e0a5d9.zip
Initial commit (split off from main spray codebase)
Diffstat (limited to 'src/main/scala/cc/spray/json/JsonPrinter.scala')
-rw-r--r--src/main/scala/cc/spray/json/JsonPrinter.scala75
1 files changed, 75 insertions, 0 deletions
diff --git a/src/main/scala/cc/spray/json/JsonPrinter.scala b/src/main/scala/cc/spray/json/JsonPrinter.scala
new file mode 100644
index 0000000..7751b7e
--- /dev/null
+++ b/src/main/scala/cc/spray/json/JsonPrinter.scala
@@ -0,0 +1,75 @@
+/*
+ * Copyright (C) 2009-2011 Mathias Doenitz
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package cc.spray.json
+
+import annotation.tailrec
+import java.lang.StringBuilder
+
+trait JsonPrinter extends (JsValue => String) {
+
+ def apply(x: JsValue) = {
+ val sb = new StringBuilder
+ print(x, sb)
+ sb.toString
+ }
+
+ def print(x: JsValue, sb: StringBuilder)
+
+ protected def printLeaf(x: JsValue, sb: StringBuilder) {
+ x match {
+ case JsNull => sb.append("null")
+ case JsTrue => sb.append("true")
+ case JsFalse => sb.append("false")
+ case JsNumber(x) => sb.append(x)
+ case JsString(x) => printString(x, sb)
+ case _ => throw new IllegalStateException
+ }
+ }
+
+ protected def printString(s: String, sb: StringBuilder) {
+ @tailrec
+ def printEscaped(s: String, ix: Int) {
+ if (ix < s.length) {
+ s.charAt(ix) match {
+ case '"' => sb.append("\\\"")
+ case '\\' => sb.append("\\\\")
+ case x if 0x20 <= x && x < 0x7F => sb.append(x)
+ case '\b' => sb.append("\\b")
+ case '\f' => sb.append("\\f")
+ case '\n' => sb.append("\\n")
+ case '\r' => sb.append("\\r")
+ case '\t' => sb.append("\\t")
+ case x if x <= 0xFF => sb.append("\\u00").append(Integer.toHexString(x))
+ case x if x <= 0xFFF => sb.append("\\u0").append(Integer.toHexString(x))
+ case x => sb.append("\\u").append(Integer.toHexString(x))
+ }
+ printEscaped(s, ix + 1)
+ }
+ }
+ sb.append('"')
+ printEscaped(s, 0)
+ sb.append('"')
+ }
+
+ protected def printSeq[A](iterable: Iterable[A], printSeparator: => Unit)(f: A => Unit) {
+ var first = true
+ iterable.foreach { a =>
+ if (first) first = false else printSeparator
+ f(a)
+ }
+ }
+} \ No newline at end of file