summary refs log tree commit diff stats
diff options
context:
space:
mode:
authordom96 <dominikpicheta@googlemail.com>2011-01-28 00:05:42 +0000
committerdom96 <dominikpicheta@googlemail.com>2011-01-28 00:05:42 +0000
commit4b7ff3e0902c92b3483bd1000a8d4fb3f2bf8077 (patch)
tree1a278f9add5e9ab1e00b0cccff641ed19a7ef189
parentfa111b9067f92107865d4496affcfc0518b7c493 (diff)
downloadNim-4b7ff3e0902c92b3483bd1000a8d4fb3f2bf8077.tar.gz
Corrected pretty() in json module, and added another file with json test data.
-rwxr-xr-xlib/pure/json.nim41
-rwxr-xr-xtests/testdata/jsontest.json2
-rw-r--r--tests/testdata/jsontest2.json80
3 files changed, 106 insertions, 17 deletions
diff --git a/lib/pure/json.nim b/lib/pure/json.nim
index 134c4926f..1afa1965b 100755
--- a/lib/pure/json.nim
+++ b/lib/pure/json.nim
@@ -18,7 +18,7 @@ import
   hashes, strutils, lexbase, streams, unicode
 
 type 
-  TJsonEventKind* = enum ## enumation of all events that may occur when parsing
+  TJsonEventKind* = enum ## enumeration of all events that may occur when parsing
     jsonError,           ## an error ocurred during parsing
     jsonEof,             ## end of file reached
     jsonString,          ## a string literal
@@ -620,21 +620,24 @@ proc toPretty(result: var string, node: PJsonNode, indent = 2, ml = True,
   of JObject:
     if currIndent != 0 and not lstArr: result.nl(ml)
     result.indent(currIndent) # Indentation
-    result.add("{")
-    result.nl(ml) # New line
-    for i in 0..len(node.fields)-1:
-      if i > 0:
-        result.add(", ")
-        result.nl(ml) # New Line
-      # Need to indent more than {
-      result.indent(newIndent(currIndent, indent, ml)) 
-      result.add(escapeJson(node.fields[i].key))
-      result.add(": ")
-      toPretty(result, node.fields[i].val, indent, ml, False, 
-               newIndent(currIndent, indent, ml))
-    result.nl(ml)
-    result.indent(currIndent) # indent the same as {
-    result.add("}")
+    if node.fields.len > 0:  
+      result.add("{")
+      result.nl(ml) # New line
+      for i in 0..len(node.fields)-1:
+        if i > 0:
+          result.add(", ")
+          result.nl(ml) # New Line
+        # Need to indent more than {
+        result.indent(newIndent(currIndent, indent, ml)) 
+        result.add(escapeJson(node.fields[i].key))
+        result.add(": ")
+        toPretty(result, node.fields[i].val, indent, ml, False, 
+                 newIndent(currIndent, indent, ml))
+      result.nl(ml)
+      result.indent(currIndent) # indent the same as {
+      result.add("}")
+    else:
+      result.add("{}")
   of JString: 
     if lstArr: result.indent(currIndent)
     result.add(escapeJson(node.str))
@@ -645,6 +648,7 @@ proc toPretty(result: var string, node: PJsonNode, indent = 2, ml = True,
     if lstArr: result.indent(currIndent)
     result.add($node.bval)
   of JArray:
+    if lstArr: result.indent(currIndent)
     if len(node.elems) != 0:
       result.add("[")
       result.nl(ml)
@@ -771,9 +775,14 @@ 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")
   echo(parsed)
   echo()
   echo(pretty(parsed, 2))
+  echo()
+  echo(parsed["keyÄÖöoßß"])
+  echo()
+  echo(pretty(parsed2))
 
   discard """
   while true:
diff --git a/tests/testdata/jsontest.json b/tests/testdata/jsontest.json
index 714d18614..3a815e50f 100755
--- a/tests/testdata/jsontest.json
+++ b/tests/testdata/jsontest.json
@@ -19,7 +19,7 @@
     
   
   ]        ,
-  "keyÄÖöoßß": false
+  "keyÄÖöoß\u00DF": false
 }
 
 // [{}, {899: 12, "x": "y"}, [], 123, 89, 89, "xyz", null, [], [], [1, 2, 3]]
diff --git a/tests/testdata/jsontest2.json b/tests/testdata/jsontest2.json
new file mode 100644
index 000000000..3a2294474
--- /dev/null
+++ b/tests/testdata/jsontest2.json
@@ -0,0 +1,80 @@
+{
+  "after": "b85008345e2faa23024b222e2e6b17f7ef8b6270", 
+  "before": "f469a6fba9a81ac06d813761e346673fbc8a6ef6", 
+  "commits": [
+    {
+      "added": [], 
+      "author": {
+        "email": "dominikpicheta@googlemail.com", 
+        "name": "dom96", 
+        "username": "dom96"
+      }, 
+      "id": "9e887b88a633c432d8e6859d7fab5429128aea7e", 
+      "message": "Added getPort and fixed a problem with the Disconnect event.", 
+      "modified": [
+        "Network\/SimpleIRC\/Core.hs"
+      ], 
+      "removed": [], 
+      "timestamp": "2010-10-24T03:16:30-07:00", 
+      "url": "https:\/\/github.com\/dom96\/SimpleIRC\/commit\/9e887b88a633c432d8e6859d7fab5429128aea7e"
+    }, 
+    {
+      "added": [
+        "tests\/channelNickTracking.hs"
+      ], 
+      "author": {
+        "email": "dominikpicheta@googlemail.com", 
+        "name": "dom96", 
+        "username": "dom96"
+      }, 
+      "id": "001c4a1834a95fa3cb4c6fefc3df9508c6e6b58a", 
+      "message": "Channel and nick tracking fully work now.", 
+      "modified": [
+        "Network\/SimpleIRC\/Core.hs", 
+        "Network\/SimpleIRC\/Messages.hs"
+      ], 
+      "removed": [], 
+      "timestamp": "2010-10-24T04:01:28-07:00", 
+      "url": "https:\/\/github.com\/dom96\/SimpleIRC\/commit\/001c4a1834a95fa3cb4c6fefc3df9508c6e6b58a"
+    }, 
+    {
+      "added": [], 
+      "author": {
+        "email": "dominikpicheta@googlemail.com", 
+        "name": "dom96", 
+        "username": "dom96"
+      }, 
+      "id": "b85008345e2faa23024b222e2e6b17f7ef8b6270", 
+      "message": "Forgot to export getPort.", 
+      "modified": [
+        "Network\/SimpleIRC\/Core.hs"
+      ], 
+      "removed": [], 
+      "timestamp": "2010-10-24T04:18:38-07:00", 
+      "url": "https:\/\/github.com\/dom96\/SimpleIRC\/commit\/b85008345e2faa23024b222e2e6b17f7ef8b6270"
+    }
+  ], 
+  "compare": "https:\/\/github.com\/dom96\/SimpleIRC\/compare\/f469a6f...b850083", 
+  "forced": false, 
+  "ref": "refs\/heads\/master", 
+  "repository": {
+    "created_at": "2010\/08\/13 10:24:57 -0700", 
+    "description": "IRC Library for Haskell", 
+    "fork": false, 
+    "forks": 1, 
+    "has_downloads": true, 
+    "has_issues": true, 
+    "has_wiki": true, 
+    "homepage": "http:\/\/hackage.haskell.org\/package\/simpleirc", 
+    "name": "SimpleIRC", 
+    "open_issues": 1, 
+    "owner": {
+      "email": "dominikpicheta@googlemail.com", 
+      "name": "dom96"
+    }, 
+    "private": false, 
+    "pushed_at": "2010\/10\/24 04:18:48 -0700", 
+    "url": "https:\/\/github.com\/dom96\/SimpleIRC", 
+    "watchers": 7
+  }
+}