Last active
August 29, 2015 14:05
-
-
Save Shu-Ji/329222634b78694953b6 to your computer and use it in GitHub Desktop.
python中json自定义dump类型
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# coding: u8 | |
import datetime | |
import json | |
_dumps = json.dumps | |
def dumps(obj, *args, **kwargs): | |
def default(obj): | |
_is = lambda type: isinstance(obj, type) | |
if _is(datetime.datetime): | |
return obj.strftime('%Y-%m-%d %H:%M:%S') | |
if _is(datetime.date): | |
return obj.strftime('%Y-%m-%d') | |
kwargs['default'] = default | |
return _dumps(obj, *args, **kwargs) | |
json.dumps = dumps | |
def main(): | |
print json.dumps(datetime.date.today()) | |
print json.dumps(datetime.datetime.now()) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment