python类JSONEncoder()的实例源码

app.py 文件源码 项目:ooni-measurements 作者: TheTorProject 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def default(self, o):
        if isinstance(o, datetime.datetime):
            if o.tzinfo:
                # eg: '2015-09-25T23:14:42.588601+00:00'
                return o.isoformat('T')
            else:
                # No timezone present - assume UTC.
                # eg: '2015-09-25T23:14:42.588601Z'
                return o.isoformat('T') + 'Z'

        if isinstance(o, datetime.date):
            return o.isoformat()

        if isinstance(o, Decimal):
            return float(o)

        return json.JSONEncoder.default(self, o)
util.py 文件源码 项目:craton 作者: openstack 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def default(self, o):
        if isinstance(o, date):
            return o.isoformat()
        return json.JSONEncoder.default(self, o)
renders.py 文件源码 项目:flask_restapi 作者: dracarysX 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def render(self):
        data = {
            'status': {
                'code': self.code,
                'message': self.message
            },
            'data': self.data
        }
        return json.dumps(data, cls=JSONEncoder, ensure_ascii=False)
widgets.py 文件源码 项目:flasky 作者: RoseOu 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def default(self, o):
            if isinstance(o, _LazyString):
                return str(o)
            return JSONEncoder.default(self, o)
renderers.py 文件源码 项目:microservices 作者: viatoriche 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def render(self, data, media_type, **options):
        # Requested indentation may be set in the Accept header.
        data = self.pre_render(data, media_type, **options)
        try:
            indent = max(min(int(media_type.params['indent']), 8), 0)
        except (KeyError, ValueError, TypeError):
            indent = None
        # Indent may be set explicitly, eg when rendered by the browsable API.
        indent = options.get('indent', indent)
        if six.PY2:  # pragma: no cover
            return json.dumps(data, cls=JSONEncoder, ensure_ascii=False,
                              indent=indent, encoding=self.charset)
        else:  # pragma: no cover
            return json.dumps(data, cls=JSONEncoder, ensure_ascii=False,
                              indent=indent)
widgets.py 文件源码 项目:oa_qian 作者: sunqb 项目源码 文件源码 阅读 17 收藏 0 点赞 0 评论 0
def default(self, o):
            if isinstance(o, _LazyString):
                return str(o)
            return JSONEncoder.default(self, o)
widgets.py 文件源码 项目:pyetje 作者: rorlika 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def default(self, o):
            if isinstance(o, _LazyString):
                return str(o)
            return JSONEncoder.default(self, o)
json.py 文件源码 项目:flask-zhenai-mongo-echarts 作者: Fretice 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def _make_encoder(superclass):
    class MongoEngineJSONEncoder(superclass):
        """
        A JSONEncoder which provides serialization of MongoEngine
        documents and queryset objects.
        """
        def default(self, obj):
            if isinstance(obj, BaseDocument):
                return json_util._json_convert(obj.to_mongo())
            elif isinstance(obj, QuerySet):
                return json_util._json_convert(obj.as_pymongo())
            return superclass.default(self, obj)
    return MongoEngineJSONEncoder
json.py 文件源码 项目:flask-zhenai-mongo-echarts 作者: Fretice 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def override_json_encoder(app):
    """
    A function to dynamically create a new MongoEngineJSONEncoder class
    based upon a custom base class.
    This function allows us to combine MongoEngine serialization with
    any changes to Flask's JSONEncoder which a user may have made
    prior to calling init_app.

    NOTE: This does not cover situations where users override
    an instance's json_encoder after calling init_app.
    """
    app.json_encoder = _make_encoder(app.json_encoder)


问题


面经


文章

微信
公众号

扫码关注公众号