diff options
-rw-r--r-- | lib/pure/json.nim | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/lib/pure/json.nim b/lib/pure/json.nim index 3599667a8..9279fea77 100644 --- a/lib/pure/json.nim +++ b/lib/pure/json.nim @@ -545,10 +545,9 @@ proc newIndent(curr, indent: int, ml: bool): int = proc nl(s: var string, ml: bool) = s.add(if ml: "\n" else: " ") -proc escapeJson*(s: string; result: var string) = - ## Converts a string `s` to its JSON representation. +proc escapeJsonUnquoted*(s: string; result: var string) = + ## Converts a string `s` to its JSON representation without quotes. ## Appends to ``result``. - result.add("\"") for c in s: case c of '\L': result.add("\\n") @@ -561,10 +560,21 @@ proc escapeJson*(s: string; result: var string) = of '\14'..'\31': result.add("\\u00" & $ord(c)) of '\\': result.add("\\\\") else: result.add(c) + +proc escapeJsonUnquoted*(s: string): string = + ## Converts a string `s` to its JSON representation without quotes. + result = newStringOfCap(s.len + s.len shr 3) + escapeJsonUnquoted(s, result) + +proc escapeJson*(s: string; result: var string) = + ## Converts a string `s` to its JSON representation with quotes. + ## Appends to ``result``. + result.add("\"") + escapeJsonUnquoted(s, result) result.add("\"") proc escapeJson*(s: string): string = - ## Converts a string `s` to its JSON representation. + ## Converts a string `s` to its JSON representation with quotes. result = newStringOfCap(s.len + s.len shr 3) escapeJson(s, result) @@ -1607,6 +1617,8 @@ when isMainModule: var parsed2 = parseFile("tests/testdata/jsontest2.json") doAssert(parsed2{"repository", "description"}.str=="IRC Library for Haskell", "Couldn't fetch via multiply nested key using {}") + doAssert escapeJsonUnquoted("\10Foo🎃barÄ") == "\\nFoo🎃barÄ" + doAssert escapeJsonUnquoted("\0\7\20") == "\\u0000\\u0007\\u0020" # for #7887 doAssert escapeJson("\10Foo🎃barÄ") == "\"\\nFoo🎃barÄ\"" doAssert escapeJson("\0\7\20") == "\"\\u0000\\u0007\\u0020\"" # for #7887 |