summaryrefslogtreecommitdiffstats
path: root/web.py
diff options
context:
space:
mode:
authorSean B. Palmer <sbp@aldebaran.local>2010-11-13 11:55:04 +0000
committerSean B. Palmer <sbp@aldebaran.local>2010-11-13 11:55:04 +0000
commit5ebe01386d46f0a3a50b2b02416d8e7ee222af7e (patch)
tree7128595c9a945d507f47d8177d8871ddc88169dc /web.py
parentca95157acbe6e340573130e45a851b39e547159d (diff)
downloadbot-5ebe01386d46f0a3a50b2b02416d8e7ee222af7e.tar.gz
bot-5ebe01386d46f0a3a50b2b02416d8e7ee222af7e.tar.bz2
bot-5ebe01386d46f0a3a50b2b02416d8e7ee222af7e.zip
Services fixes, and a standard JSON API.
Diffstat (limited to 'web.py')
-rwxr-xr-xweb.py11
1 files changed, 11 insertions, 0 deletions
diff --git a/web.py b/web.py
index 1a2b5e8..67a4843 100755
--- a/web.py
+++ b/web.py
@@ -56,5 +56,16 @@ def entity(match):
def decode(html):
return r_entity.sub(entity, html)
+r_string = re.compile(r'("(\\.|[^"\\])*")')
+r_json = re.compile(r'^[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]+$')
+env = {'__builtins__': None, 'null': None, 'true': True, 'false': False}
+
+def json(text):
+ """Evaluate JSON text safely (we hope)."""
+ if r_json.match(r_string.sub('', text)):
+ text = r_string.sub(lambda m: 'u' + m.group(1), text)
+ return eval(text.strip(' \t\r\n'), env, {})
+ raise ValueError('Input must be serialised JSON.')
+
if __name__=="__main__":
main()