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)
评论列表
文章目录