加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

flask为什么要包装json?

发布时间:2020-12-16 20:00:46 所属栏目:百科 来源:网络整理
导读: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.co

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则改要时解码了.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读