Nim: [json] Add `pairs` iterator for `JArray`

Created on 30 Aug 2018  路  5Comments  路  Source: nim-lang/Nim

Currently pairs iter works only for JsonNode objects of JObject type, not to JArray type.

See my comments here.

Proposal

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?

Feature Stdlib

Most helpful comment

Every pairs that doesn't yield (key, value) pairs but is just a workaround for the missing sugar.enumerate needs to die.

All 5 comments

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.

Was this page helpful?
0 / 5 - 0 ratings