I am trying to modify my datetime object responses in Quart so that my frontend applications receive dates in ISO8601 format.
I expected the json_provider_class
to do this job, but it's not working for me or maybe I am not doing it right.
Here's my code:
class JSONProvider(DefaultJSONProvider):
@staticmethod
def default(object_):
if isinstance(object_, (datetime.date, datetime.datetime)):
return object_.isoformat()
if isinstance(object_, (Decimal, UUID)):
return str(object_)
if is_dataclass(object_):
return asdict(object_)
if hasattr(object_, "__html__"):
return str(object_.__html__())
raise TypeError(
f"Object of type {type(object_).__name__} is not JSON serializable")
@staticmethod
def dict_to_object(dict_):
return dict_
def loads(self, object_, **kwargs):
return super().loads(object_, object_hook=self.dict_to_object, **kwargs)
app = Quart('Asaph API')
app.json_provider_class = JSONProvider
Dates are still in RFC 2822 format and I can't figure out a way to make Quart controllers return dates in ISO8601 format