From 911c5d45ecba250234a24d7647ffd72c0e59c71a Mon Sep 17 00:00:00 2001 From: Erik O'Leary Date: Fri, 15 May 2015 22:12:31 -0500 Subject: [PATCH 1/5] Improved performance of "$" on jsonnode --- lib/pure/json.nim | 58 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 49 insertions(+), 9 deletions(-) diff --git a/lib/pure/json.nim b/lib/pure/json.nim index 5d824d6f8e..9538e39d51 100644 --- a/lib/pure/json.nim +++ b/lib/pure/json.nim @@ -815,7 +815,7 @@ proc `{}`*(node: JsonNode, keys: varargs[string]): JsonNode = result = node for key in keys: if isNil(result) or result.kind!=JObject: - return nil + return nil result=result[key] proc `{}=`*(node: JsonNode, keys: varargs[string], value: JsonNode) = @@ -949,10 +949,50 @@ proc pretty*(node: JsonNode, indent = 2): string = result = "" toPretty(result, node, indent) +proc toUgly*(result: var string, node: JsonNode) = + ## Converts `node` to its JSON Representation, without + ## regard for human redability. + 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 = ## Converts `node` to its JSON Representation on one line. - result = "" - toPretty(result, node, 0, false) + result = newStringOfCap(node.len shl 1) + toUgly(result, node) iterator items*(node: JsonNode): JsonNode = ## Iterator for the items of `node`. `node` has to be a JArray. @@ -1153,7 +1193,7 @@ when false: when isMainModule: #var node = parse("{ \"test\": null }") #echo(node.existsKey("test56")) - + var parsed = parseFile("tests/testdata/jsontest.json") var parsed2 = parseFile("tests/testdata/jsontest2.json") @@ -1192,17 +1232,17 @@ when isMainModule: except: assert(false, "EInvalidIndex thrown for valid index") - assert(testJson{"b"}.str=="asd", "Couldn't fetch a singly nested key with {}") - assert(isNil(testJson{"nonexistent"}), "Non-existent keys should return nil") + assert(testJson{"b"}.str=="asd", "Couldn't fetch a singly nested key with {}") + assert(isNil(testJson{"nonexistent"}), "Non-existent keys should return nil") assert(parsed2{"repository", "description"}.str=="IRC Library for Haskell", "Couldn't fetch via multiply nested key using {}") assert(isNil(testJson{"a", "b"}), "Indexing through a list should return nil") assert(isNil(testJson{"a", "b"}), "Indexing through a list should return nil") assert(testJson{"a"}==parseJson"[1, 2, 3, 4]", "Didn't return a non-JObject when there was one to be found") assert(isNil(parseJson("[1, 2, 3]"){"foo"}), "Indexing directly into a list should return nil") - + # Generator: var j = %* [{"name": "John", "age": 30}, {"name": "Susan", "age": 31}] - assert j == %[%{"name": %"John", "age": %30}, %{"name": %"Susan", "age": %31}] + assert j == %[%{"name": %"John", "age": %30}, %{"name": %"Susan", "age": %31}] var j2 = %* [ @@ -1230,7 +1270,7 @@ when isMainModule: } ] assert j3 == %[%{"name": %"John", "age": %30}, %{"name": %"Susan", "age": %31}] - + when not defined(testing): discard """ while true: From edce0ca2edcb41e3594ac869dcd45f4edecb2ef7 Mon Sep 17 00:00:00 2001 From: onionhammer Date: Sat, 16 May 2015 11:38:43 -0500 Subject: [PATCH 2/5] Fixed typo --- lib/pure/json.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pure/json.nim b/lib/pure/json.nim index 9538e39d51..0a07da263c 100644 --- a/lib/pure/json.nim +++ b/lib/pure/json.nim @@ -951,7 +951,7 @@ proc pretty*(node: JsonNode, indent = 2): string = proc toUgly*(result: var string, node: JsonNode) = ## Converts `node` to its JSON Representation, without - ## regard for human redability. + ## regard for human readability. var comma = false case node.kind: of JArray: From f1343e52e0c079eb11027d6337ba494f22202488 Mon Sep 17 00:00:00 2001 From: onionhammer Date: Sat, 16 May 2015 12:33:00 -0500 Subject: [PATCH 3/5] Added comment noting improved performance of non-pretty string conversion --- lib/pure/json.nim | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/pure/json.nim b/lib/pure/json.nim index 0a07da263c..8389411c2d 100644 --- a/lib/pure/json.nim +++ b/lib/pure/json.nim @@ -951,7 +951,8 @@ proc pretty*(node: JsonNode, indent = 2): string = proc toUgly*(result: var string, node: JsonNode) = ## Converts `node` to its JSON Representation, without - ## regard for human readability. + ## regard for human readability. Meant to improve '$' string + ## conversion performance. var comma = false case node.kind: of JArray: From 11457bc63b53d1ba7215a19fb8663ec215a2f151 Mon Sep 17 00:00:00 2001 From: onionhammer Date: Sat, 16 May 2015 12:35:55 -0500 Subject: [PATCH 4/5] Fixed indentation (2 spaces) --- lib/pure/json.nim | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/lib/pure/json.nim b/lib/pure/json.nim index 8389411c2d..afb5fc1b1e 100644 --- a/lib/pure/json.nim +++ b/lib/pure/json.nim @@ -956,34 +956,34 @@ proc toUgly*(result: var string, node: JsonNode) = 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 "]" + 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 "{" + 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 "\"" + result.add "\"" + result.add node.str.escapeJson() + result.add "\"" of JInt: - result.add($node.num) + result.add($node.num) of JFloat: - result.add($node.fnum) + result.add($node.fnum) of JBool: - result.add(if node.bval: "true" else: "false") + result.add(if node.bval: "true" else: "false") of JNull: - result.add "null" + result.add "null" proc toUgly*(node: JsonNode): string = ## Converts `node` to its JSON Representation on one line. From 30aed77d0419916b724205b519fe54a47be6284b Mon Sep 17 00:00:00 2001 From: onionhammer Date: Sat, 16 May 2015 12:37:07 -0500 Subject: [PATCH 5/5] Fixed indentation (2 spaces) part 2 --- lib/pure/json.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pure/json.nim b/lib/pure/json.nim index afb5fc1b1e..7ab4d9ad97 100644 --- a/lib/pure/json.nim +++ b/lib/pure/json.nim @@ -971,7 +971,7 @@ proc toUgly*(result: var string, node: JsonNode) = result.add key.escapeJson() result.add "\":" result.toUgly value - result.add "}" + result.add "}" of JString: result.add "\"" result.add node.str.escapeJson()