Add escapeJsonUnquoted for json escaped strings without quotes (#8785)
* Add escapeJsonUnquoted * Add tests for escapeJsonUnquoted
This commit is contained in:
parent
3999e3be5f
commit
a270899502
1 changed files with 16 additions and 4 deletions
|
|
@ -545,10 +545,9 @@ proc newIndent(curr, indent: int, ml: bool): int =
|
||||||
proc nl(s: var string, ml: bool) =
|
proc nl(s: var string, ml: bool) =
|
||||||
s.add(if ml: "\n" else: " ")
|
s.add(if ml: "\n" else: " ")
|
||||||
|
|
||||||
proc escapeJson*(s: string; result: var string) =
|
proc escapeJsonUnquoted*(s: string; result: var string) =
|
||||||
## Converts a string `s` to its JSON representation.
|
## Converts a string `s` to its JSON representation without quotes.
|
||||||
## Appends to ``result``.
|
## Appends to ``result``.
|
||||||
result.add("\"")
|
|
||||||
for c in s:
|
for c in s:
|
||||||
case c
|
case c
|
||||||
of '\L': result.add("\\n")
|
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 '\14'..'\31': result.add("\\u00" & $ord(c))
|
||||||
of '\\': result.add("\\\\")
|
of '\\': result.add("\\\\")
|
||||||
else: result.add(c)
|
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("\"")
|
result.add("\"")
|
||||||
|
|
||||||
proc escapeJson*(s: string): string =
|
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)
|
result = newStringOfCap(s.len + s.len shr 3)
|
||||||
escapeJson(s, result)
|
escapeJson(s, result)
|
||||||
|
|
||||||
|
|
@ -1607,6 +1617,8 @@ when isMainModule:
|
||||||
var parsed2 = parseFile("tests/testdata/jsontest2.json")
|
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(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("\10Foo🎃barÄ") == "\"\\nFoo🎃barÄ\""
|
||||||
doAssert escapeJson("\0\7\20") == "\"\\u0000\\u0007\\u0020\"" # for #7887
|
doAssert escapeJson("\0\7\20") == "\"\\u0000\\u0007\\u0020\"" # for #7887
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue