An ebook/comic library service and web client
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

26 lines
571 B

  1. """
  2. Utility classes for Atheneum
  3. """
  4. from datetime import date
  5. from typing import Any
  6. import rfc3339
  7. from flask.json import JSONEncoder
  8. class CustomJSONEncoder(JSONEncoder):
  9. """
  10. Ensure that datetime values are serialized correctly
  11. """
  12. def default(self, o: Any) -> Any: # pylint: disable=E0202
  13. try:
  14. if isinstance(o, date):
  15. return rfc3339.format(o)
  16. iterable = iter(o)
  17. except TypeError:
  18. pass
  19. else:
  20. return list(iterable)
  21. return JSONEncoder.default(self, o)