| 1234567891011121314151617181920212223242526272829303132333435 |
- # coding=utf-8
- from datetime import datetime, date
- try:
- import simplejson as json
- except ImportError:
- import json
- # 方式1
- def json_time_default(obj):
- """
- >>> data = json.dumps(datetime.now(), default=json_time_default)
- """
- if isinstance(obj, datetime):
- return obj.strftime('%Y-%m-%dT%H:%M:%S')
- elif isinstance(obj, date):
- return obj.strftime('%Y-%m-%d')
- else:
- raise TypeError('%r is not JSON serializable' % obj)
- # 方式2:重写JSONEncoder类,此类负责编码,主要是通过其default函数进行转化
- class date_time_encoder(json.JSONEncoder):
- """
- >>> data = json.dumps(datetime.now(), cls=date_time_encoder)
- """
- def default(self, o):
- if isinstance(o, datetime):
- return o.strftime('%Y-%m-%dT%H:%M:%S')
- elif isinstance(o, date):
- return o.strftime('%Y-%m-%d')
- return json.JSONEncoder.default(self, o)
|