Created
May 15, 2014 12:57
-
-
Save drmalex07/5149635e6ab807c8b21e to your computer and use it in GitHub Desktop.
Create JSON dumps aware of datetime.datetime objects, in Python. #json #python #dumps #datetime
This file contains hidden or 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
import json | |
import datetime | |
'''Create an encoder subclassing JSON.encoder. | |
Make this encoder aware of our classes (e.g. datetime.datetime objects) | |
''' | |
class Encoder(json.JSONEncoder): | |
def default(self, obj): | |
if isinstance(obj, datetime): | |
return obj.isoformat() | |
else: | |
return json.JSONEncoder.default(self, obj) | |
o = { | |
'a': { | |
'boo': 'far', | |
'created': datetime.now(), | |
}, | |
'foo': 'Bar', | |
} | |
print json.dumps(o, cls=Encoder, indent=4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@Eduard-gan it's a big holywar ) IMHO we must make a new job, not repeat job that already done, of course I suppose that this job is good job.