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

Python jsonify.encode函数代码示例

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

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



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

示例1: test_detached_saobj

 def test_detached_saobj():
     s = create_session()
     t = s.query(Test1).get(1)
     # ensure it can be serialized now
     jsonify.encode(t)
     s.expunge(t)
     assert_raises(ValueError, lambda: jsonify.encode(t))
开发者ID:984958198,项目名称:tg2,代码行数:7,代码来源:test_jsonify_sqlalchemy.py


示例2: test_simple_rule

def test_simple_rule():    
    # skip this test if simplegeneric is not installed
    try:
        import simplegeneric
    except ImportError:
        return
    
    # create a Person instance
    p = Person('Jonathan', 'LaCour')
    
    # encode the object using the existing "default" rules
    result = loads(encode(p))
    assert result['first_name'] == 'Jonathan'
    assert result['last_name'] == 'LaCour'
    assert len(result) == 2
    
    # register a generic JSON rule
    @jsonify.when_type(Person)
    def jsonify_person(obj):
        return dict(
            name=obj.name
        )
    
    # encode the object using our new rule
    result = loads(encode(p))
    assert result['name'] == 'Jonathan LaCour'
    assert len(result) == 1
开发者ID:Cito,项目名称:tg2,代码行数:27,代码来源:test_generic_json.py


示例3: render_json

    def render_json(template_name, template_vars, **render_params):
        key = render_params.pop('key', None)
        if key is not None:
            template_vars = template_vars[key]

        encode = JSONRenderer._get_configured_encode(render_params)
        return encode(template_vars)
开发者ID:TurboGears,项目名称:tg2,代码行数:7,代码来源:json.py


示例4: test_nospecificjson

def test_nospecificjson():
    b = Baz()
    try:
        encoded = jsonify.encode(b)
    except TypeError as e:
        pass
    assert  "is not JSON serializable" in e.message 
开发者ID:984958198,项目名称:tg2,代码行数:7,代码来源:test_jsonify.py


示例5: test_saobj

 def test_saobj():
     s = create_session()
     t = s.query(Test1).get(1)
     encoded = jsonify.encode(t)
     expected = json.loads('{"id": 1, "val": "bob"}')
     result = json.loads(encoded)
     assert result == expected, encoded
开发者ID:984958198,项目名称:tg2,代码行数:7,代码来源:test_jsonify_sqlalchemy.py


示例6: test_select_rows

 def test_select_rows():
     s = create_session()
     t = test2.select().execute()
     encoded = jsonify.encode(dict(results=t))
     expected = json.loads("""{"results": {"count": -1, "rows": [{"count": 1, "rows": {"test1id": 1, "id": 1, "val": "fred"}}, {"count": 1, "rows": {"test1id": 1, "id": 2, "val": "alice"}}]}}""")
     result = json.loads(encoded)
     assert result == expected, encoded
开发者ID:984958198,项目名称:tg2,代码行数:7,代码来源:test_jsonify_sqlalchemy.py


示例7: test_salist

 def test_salist():
     s = create_session()
     t = s.query(Test1).get(1)
     encoded = jsonify.encode(dict(results=t.test2s))
     expected = json.loads('''{"results": [{"test1id": 1, "id": 1, "val": "fred"}, {"test1id": 1, "id": 2, "val": "alice"}]}''')
     result = json.loads(encoded)
     assert result == expected, encoded
开发者ID:984958198,项目名称:tg2,代码行数:7,代码来源:test_jsonify_sqlalchemy.py


示例8: test_simple_rule

def test_simple_rule():
    # create a Person instance
    p = Person("Jonathan", "LaCour")

    # encode the object using the existing "default" rules
    result = loads(encode(p))
    assert result["first_name"] == "Jonathan"
    assert result["last_name"] == "LaCour"
    assert len(result) == 2

    person_encoder = JSONEncoder(custom_encoders={Person: lambda p: dict(name=p.name)})

    # encode the object using our new rule
    result = loads(encode(p, encoder=person_encoder))
    assert result["name"] == "Jonathan LaCour"
    assert len(result) == 1
开发者ID:moreati,项目名称:tg2,代码行数:16,代码来源:test_generic_json.py


示例9: test_explicit_saobj

 def test_explicit_saobj():
     s = create_session()
     t = s.query(Test3).get(1)
     encoded = jsonify.encode(t)
     expected = json.loads('{"id": 1, "val": "bob", "customized": true}')
     result = json.loads(encoded)
     assert result == expected, encoded
开发者ID:984958198,项目名称:tg2,代码行数:7,代码来源:test_jsonify_sqlalchemy.py


示例10: test_datetime_time_iso

def test_datetime_time_iso():
    isodates_encoder = jsonify.JSONEncoder(isodates=True)

    d = datetime.utcnow().time()
    encoded = jsonify.encode({'date': d}, encoder=isodates_encoder)

    isoformat_without_millis = json.dumps({'date': d.isoformat()[:8]})
    assert isoformat_without_millis == encoded, (isoformat_without_millis, encoded)
开发者ID:TurboGears,项目名称:tg2,代码行数:8,代码来源:test_jsonify.py


示例11: render_jsonp

    def render_jsonp(template_name, template_vars, **kwargs):
        pname = kwargs.get('callback_param', 'callback')
        callback = tg.request.GET.get(pname)
        if callback is None:
            raise HTTPBadRequest('JSONP requires a "%s" parameter with callback name' % pname)

        values = encode(template_vars)
        return '%s(%s);' % (callback, values)
开发者ID:Cito,项目名称:tg2,代码行数:8,代码来源:json.py


示例12: test_objectid

def test_objectid():
    try:
        from bson import ObjectId
    except:
        raise SkipTest()

    d = ObjectId('507f1f77bcf86cd799439011')
    encoded = jsonify.encode({'oid':d})
    assert encoded == '{"oid": "%s"}' % d, encoded
开发者ID:984958198,项目名称:tg2,代码行数:9,代码来源:test_jsonify.py


示例13: test_select_rows

    def test_select_rows():
        s = create_session()
        t = test2.select().execute()
        encoded = jsonify.encode(t)

# this may be added back later
#
        assert encoded == '{"count": -1, "rows": [{"count": 1, "rows": {"test1id": 1, "id": 1, "val": "fred"}},\
 {"count": 1, "rows": {"test1id": 1, "id": 2, "val": "alice"}}]}', encoded
开发者ID:chiehwen,项目名称:tg2,代码行数:9,代码来源:test_jsonify_sqlalchemy.py


示例14: _get_configured_encode

    def _get_configured_encode(options):
        # Caching is not supported by JSON encoders
        options.pop('cache_expire', None)
        options.pop('cache_type', None)
        options.pop('cache_key', None)

        if not options:
            return encode
        else:
            return lambda obj: encode(obj, JSONEncoder(**options))
开发者ID:TurboGears,项目名称:tg2,代码行数:10,代码来源:json.py


示例15: test_some

    def test_some(self):
        # fields = exclude_fields(Transaction, [Transaction.user, Transaction._user_id, Transaction.expenseTagGroup_id, Transaction.incomeTagGroup_id, Transaction.expenseTagGroup, Transaction.incomeTagGroup])
        transactions = DBSession.query(Transaction).options(
            subqueryload(Transaction.incomeTagGroup).subqueryload(TagGroup.tags),
            subqueryload(Transaction.expenseTagGroup).subqueryload(TagGroup.tags)
        ).all()

        transaction_json = jsonify.encode(dict(transactions=transactions))
        parsed = json.loads(transaction_json)
        print(json.dumps(parsed, indent=2, sort_keys=True), len(transactions))
开发者ID:MarekSalat,项目名称:Trine,代码行数:10,代码来源:_test_tag.py


示例16: test_date_iso

def test_date_iso():
    isodates_encoder = jsonify.JSONEncoder(isodates=True)

    d = datetime.utcnow().date()
    encoded = jsonify.encode({'date': d}, encoder=isodates_encoder)

    isoformat_without_millis = json.dumps({'date': d.isoformat()})
    assert isoformat_without_millis == encoded, (isoformat_without_millis, encoded)

    loaded_date = json.loads(encoded)
    assert len(loaded_date['date'].split('-')) == 3
开发者ID:984958198,项目名称:tg2,代码行数:11,代码来源:test_jsonify.py


示例17: test_builtin_override

def test_builtin_override():
    # create a few date objects
    d1 = date(1979, 10, 12)
    d2 = date(2000, 1, 1)
    d3 = date(2012, 1, 1)

    # jsonify using the built in rules
    result1 = encode(dict(date=d1))
    assert '"1979-10-12"' in result1
    result2 = encode(dict(date=d2))
    assert '"2000-01-01"' in result2
    result3 = encode(dict(date=d3))
    assert '"2012-01-01"' in result3

    def jsonify_date(obj):
        if obj.year == 1979 and obj.month == 10 and obj.day == 12:
            return "Jon's Birthday!"
        elif obj.year == 2000 and obj.month == 1 and obj.day == 1:
            return "Its Y2K! Panic!"
        return "%d/%d/%d" % (obj.month, obj.day, obj.year)

    custom_date_encoder = JSONEncoder(custom_encoders={date: jsonify_date})

    # jsonify using the built in rules
    result1 = encode(dict(date=d1), encoder=custom_date_encoder)
    assert '"Jon\'s Birthday!"' in result1
    result2 = encode(dict(date=d2), encoder=custom_date_encoder)
    assert '"Its Y2K! Panic!"' in result2
    result3 = encode(dict(date=d3), encoder=custom_date_encoder)
    assert '"1/1/2012"' in result3
开发者ID:moreati,项目名称:tg2,代码行数:30,代码来源:test_generic_json.py


示例18: render_jsonp

    def render_jsonp(template_name, template_vars, **render_params):
        key = render_params.pop('key', None)
        if key is not None:
            template_vars = template_vars[key]

        pname = render_params.pop('callback_param', 'callback')
        callback = tg.request.GET.get(pname)
        if callback is None:
            raise HTTPBadRequest('JSONP requires a "%s" parameter with callback name' % pname)

        encode = JSONRenderer._get_configured_encode(render_params)
        values = encode(template_vars)
        return '%s(%s);' % (callback, values)
开发者ID:TurboGears,项目名称:tg2,代码行数:13,代码来源:json.py


示例19: test_builtin_override

def test_builtin_override():
    # skip this test if simplegeneric is not installed
    try:
        import simplegeneric
    except ImportError:
        return
    
    # create a few date objects
    d1 = date(1979, 10, 12)
    d2 = date(2000, 1, 1)
    d3 = date(2012, 1, 1)
    
    # jsonify using the built in rules
    result1 = encode(dict(date=d1))
    assert '"1979-10-12"' in result1
    result2 = encode(dict(date=d2))
    assert '"2000-01-01"' in result2
    result3 = encode(dict(date=d3))
    assert '"2012-01-01"' in result3
    
    # create a custom rule
    @jsonify.when_type(date)
    def jsonify_date(obj):
        if obj.year == 1979 and obj.month == 10 and obj.day == 12:
            return "Jon's Birthday!"
        elif obj.year == 2000 and obj.month == 1 and obj.day == 1:
            return "Its Y2K! Panic!"
        return '%d/%d/%d' % (obj.month, obj.day, obj.year)
    
    # jsonify using the built in rules
    result1 = encode(dict(date=d1))
    assert '"Jon\'s Birthday!"' in result1
    result2 = encode(dict(date=d2))
    assert '"Its Y2K! Panic!"' in result2
    result3 = encode(dict(date=d3))
    assert  '"1/1/2012"' in result3
开发者ID:Cito,项目名称:tg2,代码行数:36,代码来源:test_generic_json.py


示例20: test_datetime

def test_datetime():
    d = datetime.utcnow()
    encoded = jsonify.encode({'date':d})
    assert str(d.year) in encoded, (str(d), encoded)
开发者ID:984958198,项目名称:tg2,代码行数:4,代码来源:test_jsonify.py



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
Python webtest.test_context函数代码示例发布时间:2022-05-27
下一篇:
Python i18n._函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

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

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

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