Skip to content
This repository has been archived by the owner on Jan 2, 2024. It is now read-only.

Commit

Permalink
Feature/66 custom encoder (#69)
Browse files Browse the repository at this point in the history
* fix: add custom encoder to handle datetime serialization
  • Loading branch information
jrobinAV authored Jul 17, 2022
1 parent 53bcbb9 commit 1081c95
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
name="taipy-rest",
keywords="taipy-rest",
python_requires=">=3.8",
version="1.1.1",
version="1.1.2",
author_email="[email protected]",
packages=find_namespace_packages(where="src") + find_packages(include=["taipy", "taipy.rest"]),
package_dir={"": "src"},
Expand Down
3 changes: 3 additions & 0 deletions src/taipy/rest/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)

Expand Down
18 changes: 18 additions & 0 deletions src/taipy/rest/commons/encoder.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 1081c95

Please sign in to comment.