This commit is contained in:
Dominik Picheta
2018-03-29 11:57:53 +01:00
parent 082b071683
commit 9c2cdc94a2
2 changed files with 25 additions and 0 deletions

View File

@@ -964,6 +964,16 @@ proc `{}`*(node: JsonNode, keys: varargs[string]): JsonNode =
return nil
result = result.fields.getOrDefault(key)
proc `{}`*(node: JsonNode, index: varargs[int]): JsonNode =
## Traverses the node and gets the given value. If any of the
## indexes do not exist, returns ``nil``. Also returns ``nil`` if one of the
## intermediate data structures is not an array.
result = node
for i in index:
if isNil(result) or result.kind != JArray or i >= node.len:
return nil
result = result.elems[i]
proc getOrDefault*(node: JsonNode, key: string): JsonNode =
## Gets a field from a `node`. If `node` is nil or not an object or
## value at `key` does not exist, returns nil

View File

@@ -315,6 +315,21 @@ when isMainModule:
doAssert noYearDeser.year.isNone
doAssert noYearDeser.engine.name == "V8"
# Issue #7433
type
Obj2 = object
n1: int
n2: Option[string]
n3: bool
var j = %*[ { "n1": 4, "n2": "ABC", "n3": true },
{ "n1": 1, "n3": false },
{ "n1": 1, "n2": "XYZ", "n3": false } ]
let jDeser = j.to(seq[Obj2])
doAssert jDeser[0].n2.get() == "ABC"
doAssert jDeser[1].n2.isNone()
# Table[T, Y] support.
block:
type