json.py 文件源码

python
阅读 42 收藏 0 点赞 0 评论 0

项目:fleaker 作者: croscon 项目源码 文件源码
def default(self, obj):
        """Encode individual objects into their JSON representation.

        This method is used by :class:`flask.json.JSONEncoder` to encode
        individual items in the JSON object.

        Args:
            obj (object): Any Python object we wish to convert to JSON.

        Returns:
            str: The stringified, valid JSON representation of our provided
                object.
        """
        if isinstance(obj, decimal.Decimal):
            obj = format(obj, 'f')
            str_digit = text_type(obj)

            return (str_digit.rstrip('0').rstrip('.')
                    if '.' in str_digit
                    else str_digit)

        elif isinstance(obj, phonenumbers.PhoneNumber):
            return phonenumbers.format_number(
                obj,
                phonenumbers.PhoneNumberFormat.E164
            )

        elif isinstance(obj, pendulum.Pendulum):
            return text_type(obj)

        elif isinstance(obj, arrow.Arrow):
            return text_type(obj)

        elif isinstance(obj, (datetime.datetime, datetime.date)):
            return obj.isoformat()

        try:
            return list(iter(obj))
        except TypeError:
            pass

        return super(FleakerJSONEncoder, self).default(obj)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号