diff options
-rw-r--r-- | setup.py | 5 | ||||
-rw-r--r-- | simplejson/__init__.py | 2 | ||||
-rw-r--r-- | simplejson/jsonfilter.py | 42 |
3 files changed, 47 insertions, 2 deletions
@@ -5,7 +5,7 @@ ez_setup.use_setuptools() from setuptools import setup, find_packages -VERSION = '1.2' +VERSION = '1.3' DESCRIPTION = "Simple, fast, extensible JSON encoder/decoder for Python" LONG_DESCRIPTION = """ simplejson is a simple, fast, complete, correct and extensible @@ -45,4 +45,7 @@ setup( platforms=['any'], test_suite="nose.collector", zip_safe=True, + entry_points={ + 'paste.filter_app_factory': ['json = simplejson.jsonfilter:filter'], + }, ) 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 |