本文整理汇总了Python中tg.jsonify.encode函数的典型用法代码示例。如果您正苦于以下问题:Python encode函数的具体用法?Python encode怎么用?Python encode使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了encode函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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))
示例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
示例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)
示例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
示例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
示例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
示例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
示例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
示例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
示例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)
示例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)
示例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
示例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
示例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))
示例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))