Skip to content

Instantly share code, notes, and snippets.

@cenkalti
Created May 10, 2013 13:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cenkalti/5554540 to your computer and use it in GitHub Desktop.
Save cenkalti/5554540 to your computer and use it in GitHub Desktop.
Custom encoder and decoder class for using json module with datetime objects.
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