Currently pairs iter works only for JsonNode objects of JObject type, not to JArray type.
See my comments here.
Add pairs iter for JArray too:
iterator pairs*(node: JsonNode): tuple[idx: int, val: JsonNode] =
## Iterator for the child elements of `node`. `node` has to be a JArray.
assert node.kind == JArray
var idx = 0
for elem in items(node.elems):
yield (idx, elem)
idx += 1
.. and probably mpairs too?
Every pairs that doesn't yield (key, value) pairs but is just a workaround for the missing sugar.enumerate needs to die.
Every pairs that doesn't yield (key, value) pairs but is just a workaround for the missing sugar.enumerate needs to die.
So this can be closed then?
Well others may disagree and sugar.enumerate is missing.
This issue can be closed with latest devel version
import json
import std/enumerate
let jsonNode = parseJson"""[1, 2, 3, 4]"""
doAssert jsonNode.kind == JArray
for idx, value in enumerate(jsonNode):
echo $idx & " -> " & $value
0 -> 1
1 -> 2
2 -> 3
3 -> 4
@xflywind Thanks.
Most helpful comment
Every
pairsthat doesn't yield (key, value) pairs but is just a workaround for the missingsugar.enumerateneeds to die.