Merge pull request #2738 from onionhammer/devel
Improved performance of json rendering with `$`
This commit is contained in:
commit
0ab2dc2954
1 changed files with 50 additions and 9 deletions
|
|
@ -949,10 +949,51 @@ proc pretty*(node: JsonNode, indent = 2): string =
|
||||||
result = ""
|
result = ""
|
||||||
toPretty(result, node, indent)
|
toPretty(result, node, indent)
|
||||||
|
|
||||||
|
proc toUgly*(result: var string, node: JsonNode) =
|
||||||
|
## Converts `node` to its JSON Representation, without
|
||||||
|
## regard for human readability. Meant to improve '$' string
|
||||||
|
## conversion performance.
|
||||||
|
var comma = false
|
||||||
|
case node.kind:
|
||||||
|
of JArray:
|
||||||
|
result.add "["
|
||||||
|
for child in node.elems:
|
||||||
|
if comma: result.add ","
|
||||||
|
else: comma = true
|
||||||
|
result.toUgly child
|
||||||
|
result.add "]"
|
||||||
|
of JObject:
|
||||||
|
result.add "{"
|
||||||
|
for key, value in items(node.fields):
|
||||||
|
if comma: result.add ","
|
||||||
|
else: comma = true
|
||||||
|
result.add "\""
|
||||||
|
result.add key.escapeJson()
|
||||||
|
result.add "\":"
|
||||||
|
result.toUgly value
|
||||||
|
result.add "}"
|
||||||
|
of JString:
|
||||||
|
result.add "\""
|
||||||
|
result.add node.str.escapeJson()
|
||||||
|
result.add "\""
|
||||||
|
of JInt:
|
||||||
|
result.add($node.num)
|
||||||
|
of JFloat:
|
||||||
|
result.add($node.fnum)
|
||||||
|
of JBool:
|
||||||
|
result.add(if node.bval: "true" else: "false")
|
||||||
|
of JNull:
|
||||||
|
result.add "null"
|
||||||
|
|
||||||
|
proc toUgly*(node: JsonNode): string =
|
||||||
|
## Converts `node` to its JSON Representation on one line.
|
||||||
|
result = newStringOfCap(node.len shl 1)
|
||||||
|
toUgly(result, node)
|
||||||
|
|
||||||
proc `$`*(node: JsonNode): string =
|
proc `$`*(node: JsonNode): string =
|
||||||
## Converts `node` to its JSON Representation on one line.
|
## Converts `node` to its JSON Representation on one line.
|
||||||
result = ""
|
result = newStringOfCap(node.len shl 1)
|
||||||
toPretty(result, node, 0, false)
|
toUgly(result, node)
|
||||||
|
|
||||||
iterator items*(node: JsonNode): JsonNode =
|
iterator items*(node: JsonNode): JsonNode =
|
||||||
## Iterator for the items of `node`. `node` has to be a JArray.
|
## Iterator for the items of `node`. `node` has to be a JArray.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue