summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorhlaaf <hlaaftana@users.noreply.github.com>2018-05-29 02:15:37 +0300
committerVarriount <Varriount@users.noreply.github.com>2018-05-28 19:15:37 -0400
commit5866e64ebcc4d6a80fb3403b6a38317fe5a52e74 (patch)
treeb86840748d83ead071d1c4c07ed299c165f44298
parent66780c1f4fe4dc0d525e593c685cb7933bf81fe6 (diff)
downloadNim-5866e64ebcc4d6a80fb3403b6a38317fe5a52e74.tar.gz
fix #7881, control characters in json output (#7887)
* fix #7881, control characters in json output

* Add test for control characters in json
-rw-r--r--lib/pure/json.nim3
1 files changed, 3 insertions, 0 deletions
diff --git a/lib/pure/json.nim b/lib/pure/json.nim
index 2bb830bcb..e7ad5bd5a 100644
--- a/lib/pure/json.nim
+++ b/lib/pure/json.nim
@@ -558,6 +558,8 @@ proc escapeJson*(s: string; result: var string) =
     of '\t': result.add("\\t")
     of '\r': result.add("\\r")
     of '"': result.add("\\\"")
+    of '\0'..'\7': result.add("\\u000" & $ord(c))
+    of '\14'..'\31': result.add("\\u00" & $ord(c))
     of '\\': result.add("\\\\")
     else: result.add(c)
   result.add("\"")
@@ -1581,6 +1583,7 @@ when isMainModule:
     doAssert(parsed2{"repository", "description"}.str=="IRC Library for Haskell", "Couldn't fetch via multiply nested key using {}")
 
   doAssert escapeJson("\10Foo🎃barÄ") == "\"\\nFoo🎃barÄ\""
+  doAssert escapeJson("\0\7\20") == "\"\\u0000\\u0007\\u0020\"" # for #7887
 
   # Test with extra data
   when not defined(js):