Last active
January 6, 2021 15:12
-
-
Save setaou/ff98e82a9ce68f4c2b8637406b4620d1 to your computer and use it in GitHub Desktop.
Custom JSON encoder/decoder that handles datetime.datetime (de)serialisation
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
# -*- coding: utf-8 -*- | |
import json | |
import datetime | |
import dateutil.parser | |
import re | |
iso_datetime_regex = re.compile(r"^(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z)?)|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z)?)|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z)?)$") | |
def new_scanstring(s, end, encoding=None, strict=True): | |
(s, end) = json.decoder.scanstring(s, end, encoding, strict) | |
if iso_datetime_regex.match(s): | |
return (dateutil.parser.parse(s), end) | |
else: | |
return (s, end) | |
class JSONDecoder(json.JSONDecoder): | |
""" JSON Decoder that transforms ISO time format representations into datetime.datetime """ | |
def __init__(self, *args, **kwargs): | |
json.JSONDecoder.__init__(self, *args, **kwargs) | |
self.parse_string = new_scanstring | |
self.scan_once = json.scanner.py_make_scanner(self) # Use the python version as the C version do not use the new parse_string | |
def custom_encoder(obj): | |
if isinstance(obj, datetime.datetime): | |
return obj.isoformat() | |
else: | |
raise TypeError | |
def dumps(obj, *args, **kwargs): | |
kwargs['default'] = custom_encoder | |
return json.dumps(obj, *args, **kwargs) | |
def loads(s, *args, **kwargs): | |
kwargs['cls'] = JSONDecoder | |
return json.loads(s, *args, **kwargs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Your code is exceptional and much appreciated. I have made slight changes and updated for Python 3.7, shown here: