コード例 #1
0
 def test_render_json_with_indent(self):
     app = self._make_app()
     renderer = renderers.JSONRenderer()
     with app.app_context():
         content = renderer.render({'example': 'example'},
                                   MediaType('application/json; indent=4'))
     expected = '{\n    "example": "example"\n}'
     self.assertEqual(content, expected)
コード例 #2
0
    def test_render_json_with_custom_encoder(self):
        class CustomJsonEncoder(JSONEncoder):
            def default(self, o):
                if isinstance(o, datetime):
                    return o.isoformat()
                return super(CustomJsonEncoder, self).default(o)

        app = self._make_app()
        app.json_encoder = CustomJsonEncoder
        renderer = renderers.JSONRenderer()
        date = datetime(2017, 10, 5, 15, 22)
        with app.app_context():
            content = renderer.render(date, MediaType('application/json'))
コード例 #3
0
 def test_render_json(self):
     renderer = renderers.JSONRenderer()
     content = renderer.render({'example': 'example'}, MediaType('application/json'))
     expected = '{"example": "example"}'
     self.assertEqual(content, expected)
コード例 #4
0
 def test_render_json_with_indent(self):
     renderer = renderers.JSONRenderer()
     content = renderer.render({'example': 'example'},
                               MediaType('application/json; indent=4'))
     expected = '{\n    "example": "example"\n}'
     assert content == expected