123456789101112131415161718 |
- import datetime
- import json
- class MyEncoder(json.JSONEncoder):
- def default(self, obj):
- if isinstance(obj, datetime.datetime):
- return obj.strftime("%Y-%m-%d %H:%M:%S")
- if isinstance(obj, bytes):
- return str(obj, encoding='utf-8')
- if isinstance(obj, int):
- return int(obj)
- elif isinstance(obj, float):
- return float(obj)
- elif isinstance(obj, bool):
- return str(obj, encoding='utf-8')
- else:
- return super(MyEncoder, self).default(obj)
|