Permalink
Browse files

refactor duplicated code

-re-use the character escaping code when serializing strings
-uncomment lines that were accidentally commented in Serbench
-TODOs for the sys.errors in JsonAST. those need to go away
  • Loading branch information...
wpalmeri committed Apr 17, 2013
1 parent e4cf0a1 commit 4a637a72ddbe1086bcff5447398ad0f654deb1a1
Showing with 14 additions and 24 deletions.
  1. +6 −6 core/json/benchmark/Serbench.scala
  2. +8 −18 core/json/src/main/scala/net/liftweb/json/JsonAST.scala
@@ -41,12 +41,12 @@ object Serbench extends Benchmark {
}
def main(args: Array[String]) = {
// println("** No type hints")
// new Bench()(Serialization.formats(NoTypeHints))
// println("** Short type hints")
// new Bench()(Serialization.formats(ShortTypeHints(classes)))
// println("** Full type hints")
// new Bench()(Serialization.formats(FullTypeHints(classes)))
println("** No type hints")
new Bench()(Serialization.formats(NoTypeHints))
println("** Short type hints")
new Bench()(Serialization.formats(ShortTypeHints(classes)))
println("** Full type hints")
new Bench()(Serialization.formats(FullTypeHints(classes)))
println("** JValue Serialization")
new JValueBench()
}
@@ -396,14 +396,14 @@ object JsonAST {
case JDouble(n) => text(n.toString)
case JInt(n) => text(n.toString)
case JNull => text("null")
case JNothing => sys.error("can't render 'nothing'")
case JString(null) => text("null")
case JString(s) => text("\"" + quote(s) + "\"")
case JArray(arr) => text("[") :: series(trimArr(arr).map(render)) :: text("]")
case JField(n, v) => text("\"" + quote(n) + "\":") :: render(v)
case JObject(obj) =>
val nested = break :: fields(trimObj(obj).map(f => text("\"" + quote(f.name) + "\":") :: render(f.value)))
text("{") :: nest(2, nested) :: break :: text("}")
case JNothing => sys.error("can't render 'nothing'") //TODO: this should not throw an exception
}
private def trimArr(xs: List[JValue]) = xs.filter(_ != JNothing)
@@ -417,6 +417,11 @@ object JsonAST {
private[json] def quote(s: String): String = {
val buf = new StringBuilder
appendEscapedString(buf, s)
buf.toString
}
private def appendEscapedString(buf: StringBuilder, s: String) {
for (i <- 0 until s.length) {
val c = s.charAt(i)
buf.append(c match {
@@ -431,7 +436,6 @@ object JsonAST {
case c => c
})
}
buf.toString
}
/** Renders JSON directly to string in compact format.
@@ -454,12 +458,12 @@ object JsonAST {
case JDouble(n) => buf.append(n.toString)
case JInt(n) => buf.append(n.toString)
case JNull => buf.append("null")
case JNothing => sys.error("can't render 'nothing'")
case JString(null) => buf.append("null")
case JString(s) => bufQuote(s, buf)
case JArray(arr) => bufRenderArr(arr, buf)
case JField(k, v) => bufQuote(k, buf).append(":"); bufRender(v, buf)
case JObject(obj) => bufRenderObj(obj, buf)
case JNothing => sys.error("can't render 'nothing'") //TODO: this should not throw an exception
}
private def bufRenderArr(xs: List[JValue], buf: StringBuilder): StringBuilder = {
@@ -494,25 +498,11 @@ object JsonAST {
private def bufQuote(s: String, buf: StringBuilder): StringBuilder = {
buf.append("\"") //open quote
for (i <- 0 until s.length) {
buf.append(s.charAt(i) match {
case '"' => "\\\""
case '\\' => "\\\\"
case '\b' => "\\b"
case '\f' => "\\f"
case '\n' => "\\n"
case '\r' => "\\r"
case '\t' => "\\t"
case c if ((c >= '\u0000' && c < '\u0020')) => "\\u%04x".format(c: Int)
case c => c
})
}
appendEscapedString(buf, s)
buf.append("\"") //close quote
buf
}
}
/** Basic implicit conversions from primitive types into JSON.

0 comments on commit 4a637a7

Please sign in to comment.