summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Kögl <stefan@skoegl.net>2012-12-10 15:16:47 +0100
committerStefan Kögl <stefan@skoegl.net>2012-12-10 15:16:47 +0100
commita990cbab99e7a63820e2668990cb40507e0ae664 (patch)
tree7898ce70483a321d31ebc3fd8d60b6816fe3c7ce
parentde634ddedd1a39710abf39d99981b0adda95a5e3 (diff)
downloadpython-json-pointer-a990cbab99e7a63820e2668990cb40507e0ae664.tar.gz
proper array index validation (dash, unsigned int)
According to the spec http://tools.ietf.org/html/draft-ietf-appsawg-json-pointer-06 o If the currently referenced value is a JSON array, the reference token MUST contain either: * characters that represent an unsigned base-10 integer value (possibly with leading zeros), making the new referenced value the array element with the zero-based index identified by the token, or * exactly the single character "-", making the new referenced value the (non-existant) member after the last array element.
-rw-r--r--jsonpointer.py11
1 files changed, 8 insertions, 3 deletions
diff --git a/jsonpointer.py b/jsonpointer.py
index 041fa4a..5b118a1 100644
--- a/jsonpointer.py
+++ b/jsonpointer.py
@@ -49,6 +49,11 @@ except ImportError: # Python 3
izip = zip
from itertools import tee
+import re
+
+
+# array indices must not contain signs, spaces, decimal parts, etc
+RE_ARRAY_INDEX=re.compile('^[0-9]+$')
class JsonPointerException(Exception):
@@ -227,11 +232,11 @@ class JsonPointer(object):
if part == '-':
return part
- try:
- return int(part)
- except ValueError:
+ if not RE_ARRAY_INDEX.match(part):
raise JsonPointerException("'%s' is not a valid list index" % (part, ))
+ return int(part)
+
else:
raise JsonPointerException("Unknown document type '%s'" % (doc.__class__,))