diff options
author | Emery Hemingway <githubjunk@spam.works> | 2017-02-21 20:24:55 +0100 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2017-02-21 20:24:55 +0100 |
commit | 2ba374f9ab5f5ede0a4c67ab8339799108e3d720 (patch) | |
tree | ea322502009fbd2b187e7a15eb3f6950ce71239e | |
parent | ce4587d7b721839ffa8ed4c9d1c2abd8f308f99d (diff) | |
download | Nim-2ba374f9ab5f5ede0a4c67ab8339799108e3d720.tar.gz |
match json.toPretty style with NodeJS's stringify (#5406)
-rw-r--r-- | lib/pure/json.nim | 9 |
1 files changed, 4 insertions, 5 deletions
diff --git a/lib/pure/json.nim b/lib/pure/json.nim index 5e36a2aa1..c7b581a85 100644 --- a/lib/pure/json.nim +++ b/lib/pure/json.nim @@ -952,7 +952,7 @@ proc newIndent(curr, indent: int, ml: bool): int = else: return indent proc nl(s: var string, ml: bool) = - if ml: s.add("\n") + s.add(if ml: "\n" else: " ") proc escapeJson*(s: string; result: var string) = ## Converts a string `s` to its JSON representation. @@ -986,15 +986,14 @@ proc toPretty(result: var string, node: JsonNode, indent = 2, ml = true, lstArr = false, currIndent = 0) = case node.kind of JObject: - if currIndent != 0 and not lstArr: result.nl(ml) - result.indent(currIndent) # Indentation + if lstArr: result.indent(currIndent) # Indentation if node.fields.len > 0: result.add("{") result.nl(ml) # New line var i = 0 for key, val in pairs(node.fields): if i > 0: - result.add(", ") + result.add(",") result.nl(ml) # New Line inc i # Need to indent more than { @@ -1030,7 +1029,7 @@ proc toPretty(result: var string, node: JsonNode, indent = 2, ml = true, result.nl(ml) for i in 0..len(node.elems)-1: if i > 0: - result.add(", ") + result.add(",") result.nl(ml) # New Line toPretty(result, node.elems[i], indent, ml, true, newIndent(currIndent, indent, ml)) |