Skip to content

Instantly share code, notes, and snippets.

@abhinav-upadhyay
Last active September 10, 2024 19:58
Show Gist options
  • Save abhinav-upadhyay/5300137 to your computer and use it in GitHub Desktop.
Save abhinav-upadhyay/5300137 to your computer and use it in GitHub Desktop.
A JSON decoder/encoder implementation for parsing dates as datetime objects in Python
#!/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'])
@benjaminmgross
Copy link

Thanks for this gist @abhinav-upadhyay, this is the best quick-summary of creating subclasses of JSONEncoders and JSONDecoders I've found yet!

@i1caro
Copy link

i1caro commented Jun 1, 2016

Thanks. A beautiful implementation.

@s-c-p
Copy link

s-c-p commented Aug 14, 2017

Hey genius, mind commenting all that code?

@spattanaik75
Copy link

Thank you :) I finally got my object to successfully work with a datetime type :)

@teszeri
Copy link

teszeri commented Feb 16, 2018

Thanks! Nice solution!

@buhtz
Copy link

buhtz commented Apr 2, 2018

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).

@buhtz
Copy link

buhtz commented Apr 2, 2018

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/

@uros-r
Copy link

uros-r commented Nov 22, 2018

See https://www.techatbloomberg.com/blog/work-dates-time-python/ for slight variation that preserves timezones.

@boileaum
Copy link

Thanks. Same as @buhtz: a license would be welcome!

@abhinav-upadhyay
Copy link
Author

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.

@boileaum
Copy link

Thanks a lot!

@boileaum
Copy link

I published your solution (with slight modifications) as a pypi package: https://pypi.org/project/datetimejson/.

@buhtz
Copy link

buhtz commented Feb 15, 2023

Puh! Isn't this a bit to much to create a package for such a small piece of code?

@abhinav-upadhyay
Copy link
Author

Thanks @boileaum. I think the package is a convenience, I would use it myself next time I need this.

@boileaum
Copy link

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 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment