summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorGrzegorz Adam Hankiewicz <gradha@imap.cc>2012-12-11 21:51:03 +0100
committerGrzegorz Adam Hankiewicz <gradha@imap.cc>2012-12-11 22:30:29 +0100
commitd9dab30c1499dec081f553b887905009cd8f3e1a (patch)
tree16fa6bdad8ea62df4bb1e885ff98d6af8d4cf4ee /lib
parent3ff2f7fbbc48b1de3f8e7031dfb271862a8878fa (diff)
downloadNim-d9dab30c1499dec081f553b887905009cd8f3e1a.tar.gz
Documents json [] accesors, raises explicit exception.
Diffstat (limited to 'lib')
-rwxr-xr-xlib/pure/json.nim8
1 files changed, 6 insertions, 2 deletions
diff --git a/lib/pure/json.nim b/lib/pure/json.nim
index 941d88dfc..e7945eac3 100755
--- a/lib/pure/json.nim
+++ b/lib/pure/json.nim
@@ -607,14 +607,14 @@ proc len*(n: PJsonNode): int =
   else: nil
 
 proc `[]`*(node: PJsonNode, name: String): PJsonNode =
-  ## Gets a field from a `JObject`.
+  ## Gets a field from a `JObject`. Returns nil if the key is not found.
   assert(node.kind == JObject)
   for key, item in items(node.fields):
     if key == name:
       return item
   return nil
   
-proc `[]`*(node: PJsonNode, index: Int): PJsonNode =
+proc `[]`*(node: PJsonNode, index: Int): PJsonNode {.raises: [EInvalidIndex].} =
   ## Gets the node at `index` in an Array.
   assert(node.kind == JArray)
   return node.elems[index]
@@ -893,6 +893,10 @@ when isMainModule:
   echo(parsed["keyÄÖöoßß"])
   echo()
   echo(pretty(parsed2))
+  try:
+    echo(parsed["key2"][12123])
+    raise newException(EInvalidValue, "That line was expected to fail")
+  except EInvalidIndex: echo()
 
   discard """
   while true: