Fix json_content helper on pythons with a unicode native str type

This commit is contained in:
Martin
2012-07-27 19:52:44 +01:00
parent 3422747c06
commit da37fd0364
2 changed files with 7 additions and 3 deletions

View File

@@ -190,9 +190,13 @@ class TracebackContent(Content):
return length
def json_content(data):
def json_content(json_data):
"""Create a JSON `Content` object from JSON-encodeable data."""
return Content(JSON, lambda: [json.dumps(data)])
data = json.dumps(json_data)
if str_is_unicode:
# The json module perversely returns native str not bytes
data = data.encode('utf8')
return Content(JSON, lambda: [data])
def text_content(text):

View File

@@ -155,7 +155,7 @@ class TestContent(TestCase):
def test_json_content(self):
data = {'foo': 'bar'}
expected = Content(JSON, lambda: [json.dumps(data)])
expected = Content(JSON, lambda: [_b('{"foo": "bar"}')])
self.assertEqual(expected, json_content(data))