added json.hash

This commit is contained in:
Billingsly Wetherfordshire
2014-06-02 18:13:56 -05:00
parent 4099abc867
commit ac797e1801

View File

@@ -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.