• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python json.loads函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中restless.utils.json.loads函数的典型用法代码示例。如果您正苦于以下问题:Python loads函数的具体用法?Python loads怎么用?Python loads使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了loads函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: test_build_error

    def test_build_error(self):
        err = HttpError("Whoopsie")
        resp = self.res.build_error(err)
        resp_body = json.loads(resp.body)
        self.assertEqual(resp_body, {'error': 'Whoopsie'})
        self.assertEqual(resp.content_type, 'application/json')
        self.assertEqual(resp.status_code, 500)

        nf_err = NotFound()
        resp = self.res.build_error(nf_err)
        resp_body = json.loads(resp.body)
        # Default error message.
        self.assertEqual(resp_body, {'error': 'Resource not found.'})
        self.assertEqual(resp.content_type, 'application/json')
        # Custom status code.
        self.assertEqual(resp.status_code, 404)

        # Non-restless exception.
        unknown_err = AttributeError("'something' not found on the object.")
        resp = self.res.build_error(unknown_err)
        resp_body = json.loads(resp.body)
        # Still gets the JSON treatment & an appropriate status code.
        self.assertEqual(resp_body, {'error': "'something' not found on the object."})
        self.assertEqual(resp.content_type, 'application/json')
        self.assertEqual(resp.status_code, 500)
开发者ID:frewsxcv,项目名称:restless,代码行数:25,代码来源:test_resources.py


示例2: test_handle_not_authenticated

    def test_handle_not_authenticated(self):
        # Special-cased above for testing.
        self.res.request = FakeHttpRequest('DELETE')

        # First with DEBUG on
        resp = self.res.handle('list')
        self.assertEqual(resp['Content-Type'], 'application/json')
        self.assertEqual(resp.status_code, 401)
        resp_json = json.loads(resp.content.decode('utf-8'))
        self.assertEqual(resp_json['error'], 'Unauthorized.')
        self.assertTrue('traceback' in resp_json)

        # Now with DEBUG off.
        settings.DEBUG = False
        self.addCleanup(setattr, settings, 'DEBUG', True)
        resp = self.res.handle('list')
        self.assertEqual(resp['Content-Type'], 'application/json')
        self.assertEqual(resp.status_code, 401)
        resp_json = json.loads(resp.content.decode('utf-8'))
        self.assertEqual(resp_json, {
            'error': 'Unauthorized.',
        })
        self.assertFalse('traceback' in resp_json)

        # Last, with bubble_exceptions.
        class Bubbly(DjTestResource):
            def bubble_exceptions(self):
                return True

        with self.assertRaises(Unauthorized):
            bubb = Bubbly()
            bubb.request = FakeHttpRequest('DELETE')
            bubb.handle('list')
开发者ID:brouberol,项目名称:restless,代码行数:33,代码来源:test_dj.py


示例3: test_as_list_paginated_second_page

    def test_as_list_paginated_second_page(self):
        list_endpoint = DjTestResourcePaginatedOnePerPage(page_size=1).as_list()

        req = FakeHttpRequest('GET', get_request={'p': 2})

        resp = list_endpoint(req)
        self.assertEqual(resp['Content-Type'], 'application/json')
        self.assertEqual(resp.status_code, 200)

        self.assertEqual(
            json.loads(resp.content.decode('utf-8')),
            {
                'objects': [
                    {
                        'author': 'daniel',
                        'body': 'Stuff here.',
                        'id': 'de-faced',
                        'title': 'Another',
                    },
                ],
                'pagination': {
                    'num_pages': 3,
                    'count': 3,
                    'page': 2,
                    'start_index': 2,
                    'end_index': 2,
                    'next_page': 3,
                    'previous_page': 1,
                    'per_page': 1,
                    },
                 },
                 )
开发者ID:toastdriven,项目名称:restless,代码行数:32,代码来源:test_dj.py


示例4: test_as_list

    def test_as_list(self):
        list_endpoint = DjTestResource.as_list()
        req = FakeHttpRequest('GET')

        resp = list_endpoint(req)
        self.assertEqual(resp['Content-Type'], 'application/json')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(json.loads(resp.content.decode('utf-8')), {
            'objects': [
                {
                    'author': 'daniel',
                    'body': 'Hello world!',
                    'id': 2,
                    'title': 'First post'
                },
                {
                    'author': 'daniel',
                    'body': 'Stuff here.',
                    'id': 4,
                    'title': 'Another'
                },
                {
                    'author': 'daniel',
                    'body': "G'bye!",
                    'id': 5,
                    'title': 'Last'
                }
            ]
        })
开发者ID:brouberol,项目名称:restless,代码行数:29,代码来源:test_dj.py


示例5: test_list

    def test_list(self):
        res = self.client.get('/mydata/')

        self.assertEqual(res.content_type, 'application/json')
        self.assertEqual(res.status_code, 200)
        self.assertEqual(json.loads(res.body.decode('utf-8')), {
            'objects': [
                {
                    'author': 'viniciuscainelli',
                    'body': 'Hello world!',
                    'id': 2,
                    'title': 'First post'
                },
                {
                    'author': 'viniciuscainelli',
                    'body': 'Stuff here.',
                    'id': 4,
                    'title': 'Another'
                },
                {
                    'author': 'viniciuscainelli',
                    'body': "G'bye!",
                    'id': 5,
                    'title': 'Last'
                }
            ]
        })
开发者ID:viniciuscainelli,项目名称:restless,代码行数:27,代码来源:test_btl.py


示例6: test_as_list

 def test_as_list(self):
     resp = self.fetch("/fake", method="GET", follow_redirects=False)
     self.assertEqual(resp.headers["Content-Type"], "application/json; charset=UTF-8")
     self.assertEqual(resp.code, 200)
     self.assertEqual(
         json.loads(resp.body.decode("utf-8")),
         {"objects": [{"id": 2, "title": "First post"}, {"id": 4, "title": "Another"}, {"id": 5, "title": "Last"}]},
     )
开发者ID:tyleryates,项目名称:restless,代码行数:8,代码来源:test_tnd.py


示例7: test_handle_not_implemented

    def test_handle_not_implemented(self):
        self.res.request = FakeHttpRequest('TRACE')

        resp = self.res.handle('list')
        self.assertEqual(resp['Content-Type'], 'application/json')
        self.assertEqual(resp.status_code, 501)
        resp_json = json.loads(resp.content.decode('utf-8'))
        self.assertEqual(resp_json['error'], "Unsupported method 'TRACE' for list endpoint.")
        self.assertTrue('traceback' in resp_json)
开发者ID:brouberol,项目名称:restless,代码行数:9,代码来源:test_dj.py


示例8: test_handle_build_err

    def test_handle_build_err(self):
        # Special-cased above for testing.
        self.res.request = FakeHttpRequest("POST")
        settings.DEBUG = False
        self.addCleanup(setattr, settings, "DEBUG", True)

        resp = self.res.handle("detail")
        self.assertEqual(resp["Content-Type"], "application/json")
        self.assertEqual(resp.status_code, 500)
        self.assertEqual(json.loads(resp.content.decode("utf-8")), {"error": "This is a random & crazy exception."})
开发者ID:jphalip,项目名称:restless,代码行数:10,代码来源:test_dj.py


示例9: test_as_detail

    def test_as_detail(self):
        detail_endpoint = ItTestResource.as_detail()
        request = FakeHttpRequest('GET')

        resp = detail_endpoint(request, 'de-faced')
        self.assertEqual(resp.content_type, 'application/json')
        self.assertEqual(resp.status, 200)
        self.assertEqual(json.loads(resp.output), {
            'id': 'de-faced',
            'title': 'Another'
        })
开发者ID:mdsrosa,项目名称:restless,代码行数:11,代码来源:test_it.py


示例10: test_detail

    def test_detail(self):
        res = self.client.get('/mydata/4/')

        self.assertEqual(res.content_type, 'application/json')
        self.assertEqual(res.status_code, 200)
        self.assertEqual(json.loads(res.body.decode('utf-8')), {
            'author': 'viniciuscainelli',
            'body': 'Stuff here.',
            'id': 4,
            'title': 'Another'
        })
开发者ID:viniciuscainelli,项目名称:restless,代码行数:11,代码来源:test_btl.py


示例11: test_object_does_not_exist

    def test_object_does_not_exist(self):
        # Make sure we get a proper Not Found exception rather than a
        # generic 500.
        self.res.request = FakeHttpRequest("GET")
        settings.DEBUG = False
        self.addCleanup(setattr, settings, "DEBUG", True)

        resp = self.res.handle("detail", 1001)
        self.assertEqual(resp["Content-Type"], "application/json")
        self.assertEqual(resp.status_code, 404)
        self.assertEqual(json.loads(resp.content.decode("utf-8")), {"error": "Model with pk 1001 not found."})
开发者ID:jphalip,项目名称:restless,代码行数:11,代码来源:test_dj.py


示例12: test_as_detail

    def test_as_detail(self):
        detail_endpoint = DjTestResource.as_detail()
        req = FakeHttpRequest("GET")

        resp = detail_endpoint(req, 4)
        self.assertEqual(resp["Content-Type"], "application/json")
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(
            json.loads(resp.content.decode("utf-8")),
            {"author": "daniel", "body": "Stuff here.", "id": 4, "title": "Another"},
        )
开发者ID:jphalip,项目名称:restless,代码行数:11,代码来源:test_dj.py


示例13: test_as_detail

    def test_as_detail(self):
        detail_endpoint = FlTestResource.as_detail()
        flask.request = FakeHttpRequest('GET')

        with self.app.test_request_context('/whatever/', method='GET'):
            resp = detail_endpoint('de-faced')
            self.assertEqual(resp.headers['Content-Type'], 'application/json')
            self.assertEqual(resp.status_code, 200)
            self.assertEqual(json.loads(resp.data.decode('utf-8')), {
                'id': 'de-faced',
                'title': 'Another'
            })
开发者ID:mdsrosa,项目名称:restless,代码行数:12,代码来源:test_fl.py


示例14: test_as_view

    def test_as_view(self):
        # This would be hooked up via the URLconf...
        schema_endpoint = DjTestResource.as_view("schema", prepare_data=False)
        req = FakeHttpRequest("GET")

        resp = schema_endpoint(req)
        self.assertEqual(resp["Content-Type"], "application/json")
        self.assertEqual(resp.status_code, 200)
        schema = json.loads(resp.content.decode("utf-8"))
        self.assertEqual(sorted(list(schema["fields"].keys())), ["author", "body", "id", "title"])
        self.assertEqual(schema["fields"]["id"]["type"], "integer")
        self.assertEqual(schema["format"], "application/json")
开发者ID:jphalip,项目名称:restless,代码行数:12,代码来源:test_dj.py


示例15: test_as_detail

 def test_as_detail(self):
     resp = self.fetch(
         '/fake_async/de-faced',
         method='GET',
         follow_redirects=False
     )
     self.assertEqual(resp.headers['Content-Type'], 'application/json; charset=UTF-8')
     self.assertEqual(resp.code, 200)
     self.assertEqual(json.loads(resp.body.decode('utf-8')), {
         'id': 'de-faced',
         'title': 'Another'
     })
开发者ID:toastdriven,项目名称:restless,代码行数:12,代码来源:test_tnd.py


示例16: test_handle_build_err

    def test_handle_build_err(self):
        # Special-cased above for testing.
        self.res.request = FakeHttpRequest('POST')
        settings.DEBUG = False
        self.addCleanup(setattr, settings, 'DEBUG', True)

        resp = self.res.handle('detail')
        self.assertEqual(resp['Content-Type'], 'application/json')
        self.assertEqual(resp.status_code, 500)
        self.assertEqual(json.loads(resp.content.decode('utf-8')), {
            'error': 'This is a random & crazy exception.'
        })
开发者ID:brouberol,项目名称:restless,代码行数:12,代码来源:test_dj.py


示例17: test_object_does_not_exist

    def test_object_does_not_exist(self):
        # Make sure we get a proper Not Found exception rather than a
        # generic 500, when code raises a ObjectDoesNotExist exception.
        self.res.request = FakeHttpRequest('GET')
        settings.DEBUG = False
        self.addCleanup(setattr, settings, 'DEBUG', True)

        resp = self.res.handle('detail', 1001)
        self.assertEqual(resp['Content-Type'], 'application/json')
        self.assertEqual(resp.status_code, 404)
        self.assertEqual(json.loads(resp.content.decode('utf-8')), {
            'error': 'Model with pk 1001 not found.'
        })
开发者ID:brouberol,项目名称:restless,代码行数:13,代码来源:test_dj.py


示例18: test_as_detail

    def test_as_detail(self):
        detail_endpoint = DjTestResource.as_detail()
        req = FakeHttpRequest('GET')

        resp = detail_endpoint(req, 4)
        self.assertEqual(resp['Content-Type'], 'application/json')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(json.loads(resp.content.decode('utf-8')), {
            'author': 'daniel',
            'body': 'Stuff here.',
            'id': 4,
            'title': 'Another'
        })
开发者ID:brouberol,项目名称:restless,代码行数:13,代码来源:test_dj.py


示例19: test_http404_exception_handling

    def test_http404_exception_handling(self):
        # Make sure we get a proper Not Found exception rather than a
        # generic 500, when code raises a Http404 exception.
        res = DjTestResourceHttp404Handling()
        res.request = FakeHttpRequest('GET')
        settings.DEBUG = False
        self.addCleanup(setattr, settings, 'DEBUG', True)

        resp = res.handle('detail', 1001)
        self.assertEqual(resp['Content-Type'], 'application/json')
        self.assertEqual(resp.status_code, 404)
        self.assertEqual(json.loads(resp.content.decode('utf-8')), {
            'error': 'Model with pk 1001 not found.'
        })
开发者ID:brouberol,项目名称:restless,代码行数:14,代码来源:test_dj.py


示例20: test_handle_not_authenticated

    def test_handle_not_authenticated(self):
        # Special-cased above for testing.
        self.res.request = FakeHttpRequest("DELETE")

        with self.assertRaises(Unauthorized):
            self.res.handle("list")

        # Now with DEBUG off.
        settings.DEBUG = False
        self.addCleanup(setattr, settings, "DEBUG", True)
        resp = self.res.handle("list")
        self.assertEqual(resp["Content-Type"], "application/json")
        self.assertEqual(resp.status_code, 401)
        self.assertEqual(json.loads(resp.content.decode("utf-8")), {"error": "Unauthorized."})
开发者ID:jphalip,项目名称:restless,代码行数:14,代码来源:test_dj.py



注:本文中的restless.utils.json.loads函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python restore.restore函数代码示例发布时间:2022-05-26
下一篇:
Python models.serialize函数代码示例发布时间:2022-05-26
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap