mirror of
https://github.com/nim-lang/Nim.git
synced 2026-06-09 05:14:20 +00:00
added json.hash
This commit is contained in:
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user