Skip to content

Instantly share code, notes, and snippets.

@Flushot
Last active December 18, 2015 12:39
Show Gist options
  • Save Flushot/5784832 to your computer and use it in GitHub Desktop.
Save Flushot/5784832 to your computer and use it in GitHub Desktop.
Equivalent of json.dumps() but includes ISO-8601 date encodings of datetime and date data.
import json
import datetime
from dateutil import tz
from naive_aware import isAware, makeAware, makeNaive
def jdumps(obj):
return json.dumps(obj,
default=lambda o: (makeAware(o) if isinstance(o, datetime.datetime) else o).isoformat() \
if (isinstance(o, datetime.datetime) or isinstance(o, datetime.date)) else None,
sort_keys=True,
indent=4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment