-
-
Save abhinav-upadhyay/5300137 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python | |
# An example of decoding/encoding datetime values in JSON data in Python. | |
# Code adapted from: http://broadcast.oreilly.com/2009/05/pymotw-json.html | |
# Copyright (c) 2023, Abhinav Upadhyay | |
# All rights reserved. | |
# | |
# Redistribution and use in source and binary forms, with or without | |
# modification, are permitted provided that the following conditions are met: | |
# | |
# 1. Redistributions of source code must retain the above copyright notice, this | |
# list of conditions and the following disclaimer. | |
# | |
# 2. Redistributions in binary form must reproduce the above copyright notice, | |
# this list of conditions and the following disclaimer in the documentation | |
# and/or other materials provided with the distribution. | |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | |
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | |
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
from datetime import datetime | |
import json | |
from json import JSONDecoder | |
from json import JSONEncoder | |
obj = {'name':'foo', 'type': 'bar','date':datetime.now()} | |
class DateTimeDecoder(json.JSONDecoder): | |
def __init__(self, *args, **kargs): | |
JSONDecoder.__init__(self, object_hook=self.dict_to_object, | |
*args, **kargs) | |
def dict_to_object(self, d): | |
if '__type__' not in d: | |
return d | |
type = d.pop('__type__') | |
try: | |
dateobj = datetime(**d) | |
return dateobj | |
except: | |
d['__type__'] = type | |
return d | |
class DateTimeEncoder(JSONEncoder): | |
""" Instead of letting the default encoder convert datetime to string, | |
convert datetime objects into a dict, which can be decoded by the | |
DateTimeDecoder | |
""" | |
def default(self, obj): | |
if isinstance(obj, datetime): | |
return { | |
'__type__' : 'datetime', | |
'year' : obj.year, | |
'month' : obj.month, | |
'day' : obj.day, | |
'hour' : obj.hour, | |
'minute' : obj.minute, | |
'second' : obj.second, | |
'microsecond' : obj.microsecond, | |
} | |
else: | |
return JSONEncoder.default(self, obj) | |
j = json.loads(json.dumps(obj,cls=DateTimeEncoder), cls=DateTimeDecoder) | |
print j['date'] | |
print type(j['date']) |
Thanks. A beautiful implementation.
Hey genius, mind commenting all that code?
Thank you :) I finally got my object to successfully work with a datetime type :)
Thanks! Nice solution!
Can you give licence informations about this code please. I would like to use (and modify a bit) your code in my GPL application (not yet released).
The link http://broadcast.oreilly.com/2009/05/pymotw-json.html is not working anymore. Can you update it?
Could it be this one? https://doughellmann.com/blog/2009/05/24/pymotw-json/
See https://www.techatbloomberg.com/blog/work-dates-time-python/ for slight variation that preserves timezones.
Thanks. Same as @buhtz: a license would be welcome!
Thanks everyone on the feedback and positive comments. Added a 2 clause BSD license. I think it should work for everyone, whether they want to include it in commercial application or open source.
Thanks a lot!
I published your solution (with slight modifications) as a pypi package: https://pypi.org/project/datetimejson/.
Puh! Isn't this a bit to much to create a package for such a small piece of code?
Thanks @boileaum. I think the package is a convenience, I would use it myself next time I need this.
Puh! Isn't this a bit to much to create a package for such a small piece of code?
I agree but I have already 3 projects using it so I prefer to have it in a common package. Moreover, I am new to python packaging using github actions so it was a good exercise 🙂
Thanks for this gist @abhinav-upadhyay, this is the best quick-summary of creating subclasses of
JSONEncoder
s andJSONDecoder
s I've found yet!