summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorErik O'Leary <erik.m.oleary@gmail.com>2015-05-15 22:12:31 -0500
committerErik O'Leary <erik.m.oleary@gmail.com>2015-05-15 22:12:31 -0500
commit911c5d45ecba250234a24d7647ffd72c0e59c71a (patch)
tree4de41f6911f29dcd08962636a8a365b67bc2cdf7 /lib
parentc30d7c3208c1da91e3f71be7d8d9f5149f3031c4 (diff)
downloadNim-911c5d45ecba250234a24d7647ffd72c0e59c71a.tar.gz
Improved performance of "$" on jsonnode
Diffstat (limited to 'lib')
-rw-r--r--lib/pure/json.nim58
1 files changed, 49 insertions, 9 deletions
diff --git a/lib/pure/json.nim b/lib/pure/json.nim
index 5d824d6f8..9538e39d5 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: