summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarcel Hellkamp <marc@gsites.de>2010-04-26 18:50:05 +0200
committerMarcel Hellkamp <marc@gsites.de>2010-04-26 18:50:05 +0200
commita3f61e4400f991f8df3820b56a654858370c6005 (patch)
tree658c389f40567ed2e2d288236d9d67ddf188793a
parent43062fb2a9a0f6221a0d47cba3f971fffc0e2a4f (diff)
downloadbottle-a3f61e4400f991f8df3820b56a654858370c6005.tar.gz
Bugfix: Autojson for empty dicts (see #65)
-rw-r--r--bottle.py8
-rw-r--r--test/test_wsgi.py7
2 files changed, 11 insertions, 4 deletions
diff --git a/bottle.py b/bottle.py
index ec06939..9edabae 100644
--- a/bottle.py
+++ b/bottle.py
@@ -272,7 +272,10 @@ class Bottle(object):
on HEAD requests.
Supports: False, str, unicode, list(unicode), dict(), open()
"""
- if not out:
+ if self.autojson and json_dumps and isinstance(out, dict):
+ out = [json_dumps(out)]
+ response.content_type = 'application/json'
+ elif not out:
out = []
response.header['Content-Length'] = '0'
elif isinstance(out, types.StringType):
@@ -281,9 +284,6 @@ class Bottle(object):
out = [out.encode(response.charset)]
elif isinstance(out, list) and isinstance(out[0], unicode):
out = map(lambda x: x.encode(response.charset), out)
- elif self.autojson and json_dumps and isinstance(out, dict):
- out = [json_dumps(out)]
- response.content_type = 'application/json'
elif hasattr(out, 'read'):
out = request.environ.get('wsgi.file_wrapper',
lambda x: iter(lambda: x.read(8192), ''))(out)
diff --git a/test/test_wsgi.py b/test/test_wsgi.py
index 5e4699f..a2f0c88 100644
--- a/test/test_wsgi.py
+++ b/test/test_wsgi.py
@@ -184,6 +184,13 @@ class TestWsgi(WsgiTestBase):
self.assertEqual(r'{"a":1}', ''.join(self.urlopen('/json').read().split()))
self.assertEqual('application/json', self.urlopen('/json').info().getheader('Content-Type',''))
+ def test_emptyjson(self):
+ """ WSGI: Autojson feature for empty dicts """
+ @bottle.route('/json')
+ def test(): return {}
+ self.assertEqual(r'{}', ''.join(self.urlopen('/json').read().split()))
+ self.assertEqual('application/json', self.urlopen('/json').info().getheader('Content-Type',''))
+
def test_cookie(self):
""" WSGI: Cookies """
@bottle.route('/cookie')