flask为什么要包装json?

前端之家收集整理的这篇文章主要介绍了flask为什么要包装json?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

flask为什么要包装json?

原文是这样解释的

on top of that it will delegate access to the current application’s json encoders and decoders for easier customization.

解释定制json输入输出.

一些源码

def jsonify(*args,**kwargs):
    if current_app.config['jsonify_prettyprint_regular'] or current_app.debug:
        indent = 2
        separators = (',',': ')
if args and kwargs:
    raise typeerror('jsonify() behavior undefined when passed both args and kwargs')
elif len(args) == 1:  # single args are passed directly to dumps()
    data = args[0]
else:
    data = args or kwargs

return current_app.response_class(
    (dumps(data,indent=indent,separators=separators),'\n'),mimetype=current_app.config['jsonify_mimetype']
)

def dumps(obj,kwargs):
_dump_arg_defaults(kwargs)
encoding = kwargs.pop('encoding',none)
rv = _json.dumps(obj,
kwargs)
if encoding is not none and isinstance(rv,text_type):
rv = rv.encode(encoding)
return rv

def loads(s,kwargs):
_load_arg_defaults(kwargs)
if isinstance(s,bytes):
s = s.decode(kwargs.pop('encoding',none) or 'utf-8')
return _json.loads(s,
kwargs)

可以看出

  • 对jsonify对返回值处理一下.

  • dumps则必要时转换了编码.

  • 同理loads则改要时解码了.

猜你在找的Json相关文章