summaryrefslogtreecommitdiff
path: root/simplejson
diff options
context:
space:
mode:
authorBob Ippolito <bob@redivi.com>2006-01-20 04:04:19 +0000
committerBob Ippolito <bob@redivi.com>2006-01-20 04:04:19 +0000
commitc4010d6d7fd0fcab8391cd1e4d6477192b2055f5 (patch)
treebe12faa2de28e152dc1b5f0668f8f067422b819b /simplejson
parent07e70f336381a0b89177feaf6e5c6af5dd8e74c2 (diff)
downloadsimplejson-c4010d6d7fd0fcab8391cd1e4d6477192b2055f5.tar.gz
jsonfilter
git-svn-id: http://simplejson.googlecode.com/svn/trunk@13 a4795897-2c25-0410-b006-0d3caba88fa1
Diffstat (limited to 'simplejson')
-rw-r--r--simplejson/__init__.py2
-rw-r--r--simplejson/jsonfilter.py42
2 files changed, 43 insertions, 1 deletions
diff --git a/simplejson/__init__.py b/simplejson/__init__.py
index 21d4915..9d2c64b 100644
--- a/simplejson/__init__.py
+++ b/simplejson/__init__.py
@@ -57,7 +57,7 @@ Extending JSONEncoder::
Note that the JSON produced by this module is a subset of YAML,
so it may be used as a serializer for that as well.
"""
-__version__ = '1.2'
+__version__ = '1.3'
__all__ = [
'dump', 'dumps', 'load', 'loads',
'JSONDecoder', 'JSONEncoder',
diff --git a/simplejson/jsonfilter.py b/simplejson/jsonfilter.py
new file mode 100644
index 0000000..7396da5
--- /dev/null
+++ b/simplejson/jsonfilter.py
@@ -0,0 +1,42 @@
+import simplejson
+import cgi
+
+class JSONFilter(object):
+ def __init__(self, app, mime_type='text/x-json'):
+ self.app = app
+ self.mime_type = mime_type
+
+ def __call__(self, environ, start_response):
+ # Read JSON POST input to jsonfilter.json if matching mime type
+ response = {'status': '200 OK', 'headers': []}
+ def json_start_response(status, headers):
+ response['status'] = status
+ response['headers'].extend(headers)
+ environ['jsonfilter.mime_type'] = self.mime_type
+ if environ.get('REQUEST_METHOD', '') == 'POST':
+ if environ.get('CONTENT_TYPE', '') == self.mime_type:
+ args = [_ for _ in [environ.get('CONTENT_LENGTH')] if _]
+ data = environ['wsgi.input'].read(*map(int, args))
+ environ['jsonfilter.json'] = simplejson.loads(data)
+ res = simple_json.dumps(app(environ, json_start_response))
+ jsonp = res.parse_qs(environ.get('QUERY_STRING', '')).get('jsonp')
+ if jsonp:
+ content_type = 'text/javascript'
+ res = ''.join(jsonp + ['(', res, ')'])
+ elif 'Opera' in environ.get('HTTP_USER_AGENT', ''):
+ # Opera has bunk XMLHttpRequest support for most mime types
+ content_type = 'text/plain'
+ else:
+ content_type = self.mime_type
+ headers = [
+ ('Content-type', content_type),
+ ('Content-length', len(res)),
+ ]
+ headers.extend(response['headers'])
+ start_response(response['status'], headers)
+ return [res]
+
+def jsonfilter_factory(global_conf, **kw):
+ def make_filter(app):
+ return JSONFilter(app, **kw)
+ return make_filter