summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2014-06-05 23:43:51 +0200
committerAndreas Rumpf <rumpf_a@web.de>2014-06-05 23:43:51 +0200
commit2dba171acc5bb3c751e5aded6754b45d0365462e (patch)
treed5129da2f2d108fdeb9b670e78754eae474d72db
parent361b9fe468a317982edf1c4a2e66b42dda0790c1 (diff)
parentac797e1801b7b24df95b1513c37c4e2813f27c5a (diff)
downloadNim-2dba171acc5bb3c751e5aded6754b45d0365462e.tar.gz
Merge pull request #1174 from fowlmouth/patch-2
added `==` for PJsonNode
-rw-r--r--lib/pure/json.nim38
1 files changed, 38 insertions, 0 deletions
diff --git a/lib/pure/json.nim b/lib/pure/json.nim
index 306a8a2e2..0476dc2ae 100644
--- a/lib/pure/json.nim
+++ b/lib/pure/json.nim
@@ -619,6 +619,44 @@ proc `%`*(elements: openArray[PJsonNode]): PJsonNode =
   newSeq(result.elems, elements.len)
   for i, p in pairs(elements): result.elems[i] = p
 
+proc `==`* (a,b: PJsonNode): bool =
+  ## Check two nodes for equality
+  if a.kind != b.kind: false
+  else:
+    case a.kind
+    of JString:
+      a.str == b.str
+    of JInt:
+      a.num == b.num
+    of JFloat:
+      a.fnum == b.fnum
+    of JBool:
+      a.bval == b.bval
+    of JNull:
+      true
+    of JArray:
+      a.elems == b.elems
+    of JObject:
+      a.fields == b.fields
+
+proc hash* (n:PJsonNode): THash =
+  ## Compute the hash for a JSON node
+  case n.kind
+  of jArray:
+    result = hash(n.elems)
+  of jObject:
+    result = hash(n.fields)
+  of jInt:
+    result = hash(n.num)
+  of jFloat:
+    result = hash(n.fnum)
+  of jBool:
+    result = hash(n.bval.int)
+  of jString:
+    result = hash(n.str)
+  of jNull:
+    result = hash(0)
+
 proc len*(n: PJsonNode): int = 
   ## If `n` is a `JArray`, it returns the number of elements.
   ## If `n` is a `JObject`, it returns the number of pairs.