Created
May 10, 2013 13:54
-
-
Save cenkalti/5554540 to your computer and use it in GitHub Desktop.
Custom encoder and decoder class for using json module with datetime objects.
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
from __future__ import absolute_import | |
import json | |
import datetime | |
DATETIME_FORMAT = "%Y-%m-%dT%H:%M:%SZ" # ISO 8601 | |
class JSONEncoder(json.JSONEncoder): | |
def default(self, obj): | |
if isinstance(obj, datetime.datetime): | |
return obj.strftime(DATETIME_FORMAT) | |
else: | |
return super(JSONEncoder, self).default(obj) | |
class JSONDecoder(json.JSONDecoder): | |
def __init__(self, *args, **kwargs): | |
json.JSONDecoder.__init__(self, *args, | |
object_hook=self.object_hook, **kwargs) | |
def object_hook(self, obj): | |
if isinstance(obj, dict): | |
for key in obj: | |
if not isinstance(obj[key], basestring): | |
continue | |
try: | |
obj[key] = datetime.datetime.strptime(obj[key], | |
DATETIME_FORMAT) | |
except ValueError: | |
pass | |
return obj | |
if __name__ == "__main__": | |
encoder = JSONEncoder() | |
print encoder.encode({"date": datetime.datetime.now()}) | |
decoder = JSONDecoder() | |
print decoder.decode(encoder.encode({"date": datetime.datetime.now()})) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment