diff options
author | Johannes Rudolph <johannes.rudolph@gmail.com> | 2014-03-12 13:12:25 +0100 |
---|---|---|
committer | Johannes Rudolph <johannes.rudolph@gmail.com> | 2014-03-12 13:12:25 +0100 |
commit | 3180cfafbaa91ada879a3bd7e1be6f3064c14cc5 (patch) | |
tree | 9ad6a29edae9531254df9bdb27b1926a604708b5 | |
parent | 615f60aa6e8e9481ada227bc0a6db76ba8c34920 (diff) | |
download | spray-json-3180cfafbaa91ada879a3bd7e1be6f3064c14cc5.tar.gz spray-json-3180cfafbaa91ada879a3bd7e1be6f3064c14cc5.tar.bz2 spray-json-3180cfafbaa91ada879a3bd7e1be6f3064c14cc5.zip |
match spec tightly about which characters to encode, fixes #83, #46
-rw-r--r-- | src/main/scala/spray/json/JsonPrinter.scala | 25 | ||||
-rw-r--r-- | src/test/scala/spray/json/CompactPrinterSpec.scala | 10 | ||||
-rw-r--r-- | src/test/scala/spray/json/PrettyPrinterSpec.scala | 2 |
3 files changed, 16 insertions, 21 deletions
diff --git a/src/main/scala/spray/json/JsonPrinter.scala b/src/main/scala/spray/json/JsonPrinter.scala index bc1e375..d255e03 100644 --- a/src/main/scala/spray/json/JsonPrinter.scala +++ b/src/main/scala/spray/json/JsonPrinter.scala @@ -97,21 +97,12 @@ trait JsonPrinter extends (JsValue => String) { } object JsonPrinter { - private[this] val mask = new Array[Int](4) - private[this] def ascii(c: Char): Int = c & ((c - 127) >> 31) // branchless for `if (c <= 127) c else 0` - private[this] def mark(c: Char): Unit = { - val b = ascii(c) - mask(b >> 5) |= 1 << (b & 0x1F) - } - private[this] def mark(range: scala.collection.immutable.NumericRange[Char]): Unit = range foreach (mark) - - mark('\u0000' to '\u001f') - mark('\u007f') - mark('"') - mark('\\') - - def requiresEncoding(c: Char): Boolean = { - val b = ascii(c) - (mask(b >> 5) & (1 << (b & 0x1F))) != 0 - } + def requiresEncoding(c: Char): Boolean = + // from RFC 4627 + // unescaped = %x20-21 / %x23-5B / %x5D-10FFFF + c match { + case '"' => true + case '\\' => true + case c => c < 0x20 + } }
\ No newline at end of file diff --git a/src/test/scala/spray/json/CompactPrinterSpec.scala b/src/test/scala/spray/json/CompactPrinterSpec.scala index 79c0877..6a9560b 100644 --- a/src/test/scala/spray/json/CompactPrinterSpec.scala +++ b/src/test/scala/spray/json/CompactPrinterSpec.scala @@ -49,12 +49,16 @@ class CompactPrinterSpec extends Specification { CompactPrinter(JsString("xyz")) mustEqual "\"xyz\"" } "properly escape special chars in JsString" in { - CompactPrinter(JsString("\"\\\b\f\n\r\t\u12AB")) mustEqual """"\"\\\b\f\n\r\t""" + "\\u12ab\"" - CompactPrinter(JsString("\u1000")) mustEqual "\"\\u1000\"" - CompactPrinter(JsString("\u0100")) mustEqual "\"\\u0100\"" + CompactPrinter(JsString("\"\\\b\f\n\r\t")) mustEqual """"\"\\\b\f\n\r\t"""" + CompactPrinter(JsString("\u1000")) mustEqual "\"\u1000\"" + CompactPrinter(JsString("\u0100")) mustEqual "\"\u0100\"" CompactPrinter(JsString("\u0010")) mustEqual "\"\\u0010\"" CompactPrinter(JsString("\u0001")) mustEqual "\"\\u0001\"" CompactPrinter(JsString("\u001e")) mustEqual "\"\\u001e\"" + // don't escape as it isn't required by the spec + CompactPrinter(JsString("\u007f")) mustEqual "\"\u007f\"" + CompactPrinter(JsString("飞机因此受到损伤")) mustEqual "\"飞机因此受到损伤\"" + CompactPrinter(JsString("\uD834\uDD1E")) mustEqual "\"\uD834\uDD1E\"" } "properly print a simple JsObject" in ( CompactPrinter(JsObject("key" -> JsNumber(42), "key2" -> JsString("value"))) diff --git a/src/test/scala/spray/json/PrettyPrinterSpec.scala b/src/test/scala/spray/json/PrettyPrinterSpec.scala index 8b7bc2b..27137a8 100644 --- a/src/test/scala/spray/json/PrettyPrinterSpec.scala +++ b/src/test/scala/spray/json/PrettyPrinterSpec.scala @@ -58,7 +58,7 @@ class PrettyPrinterSpec extends Specification { | "no": 0 | }, ["a", "b", null], false] | } - |}""".stripMargin.replace("\u00f8", "\\u00f8") + |}""".stripMargin } } } |