Last active
September 10, 2024 19:58
-
-
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
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
#!/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']) |
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
Thanks @boileaum. I think the package is a convenience, I would use it myself next time I need this.