diff options
author | Yuriy Glukhov <yutiy.glukhov@gmail.com> | 2016-04-05 22:55:00 +0300 |
---|---|---|
committer | Yuriy Glukhov <yutiy.glukhov@gmail.com> | 2016-04-06 14:16:30 +0300 |
commit | 181c834a932ac98bc4451c6f0110cf9816b9808b (patch) | |
tree | 9608621439cd22f1c686ef12533bc49dd14b2d32 | |
parent | a3a0812c89b389bdcac4b7b9efa11f72cc23855b (diff) | |
download | Nim-181c834a932ac98bc4451c6f0110cf9816b9808b.tar.gz |
Added json.getOrDefault along with {singleKey} tr optimization.
-rw-r--r-- | lib/pure/json.nim | 8 |
1 files changed, 8 insertions, 0 deletions
diff --git a/lib/pure/json.nim b/lib/pure/json.nim index 5be098664..b9da8a0dd 100644 --- a/lib/pure/json.nim +++ b/lib/pure/json.nim @@ -858,6 +858,14 @@ proc `{}`*(node: JsonNode, keys: varargs[string]): JsonNode = return nil result = result.fields.getOrDefault(key) +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 + if not isNil(node) and node.kind == JObject: + result = node.fields.getOrDefault(key) + +template simpleGetOrDefault*{`{}`(node, [key])}(node: JsonNode, key: string): JsonNode = node.getOrDefault(key) + proc `{}=`*(node: JsonNode, keys: varargs[string], value: JsonNode) = ## Traverses the node and tries to set the value at the given location ## to ``value``. If any of the keys are missing, they are added. |