Files
edx-platform/openedx/core/lib/json_utils.py
Usama Sadiq 8de47ef51f refactor: Ran pyupgrade on openedx/core
Ran pyupgrade on openedx/core/{djangolib, lib}
2021-04-08 18:34:24 +05:00

31 lines
880 B
Python

"""
Helpers for json serialization
"""
import datetime
from django.core.serializers.json import DjangoJSONEncoder
from opaque_keys.edx.keys import CourseKey, UsageKey
class EdxJSONEncoder(DjangoJSONEncoder):
"""
Custom JSONEncoder that handles `Location` and `datetime.datetime` objects.
`Location`s are encoded as their url string form, and `datetime`s as
ISO date strings
"""
def default(self, o): # pylint: disable=method-hidden
if isinstance(o, (CourseKey, UsageKey)):
return str(o)
elif isinstance(o, datetime.datetime):
if o.tzinfo is not None:
if o.utcoffset() is None:
return o.isoformat() + 'Z'
else:
return o.isoformat()
else:
return o.isoformat()
else:
return super().default(o)