diff --git a/setup.py b/setup.py index b356987..eb961da 100644 --- a/setup.py +++ b/setup.py @@ -20,7 +20,7 @@ name="taipy-rest", keywords="taipy-rest", python_requires=">=3.8", - version="1.1.1", + version="1.1.2", author_email="dev@taipy.io", packages=find_namespace_packages(where="src") + find_packages(include=["taipy", "taipy.rest"]), package_dir={"": "src"}, diff --git a/src/taipy/rest/app.py b/src/taipy/rest/app.py index 23aff3f..12bd4b4 100644 --- a/src/taipy/rest/app.py +++ b/src/taipy/rest/app.py @@ -15,6 +15,7 @@ from . import api from .extensions import apispec +from .commons.encoder import _CustomEncoder def create_app(testing=False, flask_env=None, secret_key=None): @@ -26,6 +27,8 @@ def create_app(testing=False, flask_env=None, secret_key=None): SECRET_KEY=os.getenv("SECRET_KEY", secret_key), ) + app.config["RESTFUL_JSON"] = {'cls': _CustomEncoder} + configure_apispec(app) register_blueprints(app) diff --git a/src/taipy/rest/commons/encoder.py b/src/taipy/rest/commons/encoder.py new file mode 100644 index 0000000..7730cff --- /dev/null +++ b/src/taipy/rest/commons/encoder.py @@ -0,0 +1,18 @@ +import json +from typing import Any, Union +from datetime import datetime +from enum import Enum + + + +Json = Union[dict, list, str, int, float, bool, None] + +class _CustomEncoder(json.JSONEncoder): + def default(self, o: Any) -> Json: + if isinstance(o, Enum): + result = o.value + elif isinstance(o, datetime): + result = {"__type__": "Datetime", "__value__": o.isoformat()} + else: + result = json.JSONEncoder.default(self, o) + return result