From 4099abc8675ca37e713098c211bfe73aabd34259 Mon Sep 17 00:00:00 2001 From: Billingsly Wetherfordshire Date: Sat, 3 May 2014 16:49:41 -0500 Subject: [PATCH 1/2] added `==` for PJsonNode --- lib/pure/json.nim | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lib/pure/json.nim b/lib/pure/json.nim index 4250847e56..b325c2905d 100644 --- a/lib/pure/json.nim +++ b/lib/pure/json.nim @@ -619,6 +619,25 @@ 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 = + 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 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. From ac797e1801b7b24df95b1513c37c4e2813f27c5a Mon Sep 17 00:00:00 2001 From: Billingsly Wetherfordshire Date: Mon, 2 Jun 2014 18:13:56 -0500 Subject: [PATCH 2/2] added json.hash --- lib/pure/json.nim | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lib/pure/json.nim b/lib/pure/json.nim index b325c2905d..c565898036 100644 --- a/lib/pure/json.nim +++ b/lib/pure/json.nim @@ -620,6 +620,7 @@ proc `%`*(elements: openArray[PJsonNode]): PJsonNode = 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 @@ -638,6 +639,24 @@ proc `==`* (a,b: PJsonNode): bool = 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.