Skip to content

Instantly share code, notes, and snippets.

@nom3ad
Forked from simonw/gist:7000493
Created June 9, 2017 11:21
Show Gist options
  • Save nom3ad/b0c058e8d6b419d240f98ef353ec48c2 to your computer and use it in GitHub Desktop.
Save nom3ad/b0c058e8d6b419d240f98ef353ec48c2 to your computer and use it in GitHub Desktop.
How to use custom Python JSON serializers and deserializers to automatically roundtrip complex types.
import json, datetime
class RoundTripEncoder(json.JSONEncoder):
DATE_FORMAT = "%Y-%m-%d"
TIME_FORMAT = "%H:%M:%S"
def default(self, obj):
if isinstance(obj, datetime.datetime):
return {
"_type": "datetime",
"value": obj.strftime("%s %s" % (
self.DATE_FORMAT, self.TIME_FORMAT
))
}
return super(RoundTripEncoder, self).default(obj)
data = {
"name": "Silent Bob",
"dt": datetime.datetime(2013, 11, 11, 10, 40, 32)
}
print json.dumps(data, cls=RoundTripEncoder, indent=2)
import json, datetime
from dateutil import parser
class RoundTripDecoder(json.JSONDecoder):
def __init__(self, *args, **kwargs):
json.JSONDecoder.__init__(self, object_hook=self.object_hook, *args, **kwargs)
def object_hook(self, obj):
if '_type' not in obj:
return obj
type = obj['_type']
if type == 'datetime':
return parser.parse(obj['value'])
return obj
print json.loads(s, cls=RoundTripDecoder)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment