diff --git a/setup.cfg b/setup.cfg index 2717bfd313..edd8bcd853 100644 --- a/setup.cfg +++ b/setup.cfg @@ -56,6 +56,7 @@ install_requires = matterhook==0.2 meilisearch==0.28.3 OpenTimelineIO==0.15.0 + orjson==3.9.7 pillow==10.0.1 psutil==5.9.5 psycopg[binary]==3.1.11 diff --git a/zou/app/__init__.py b/zou/app/__init__.py index 3a832411e4..2dd2852471 100644 --- a/zou/app/__init__.py +++ b/zou/app/__init__.py @@ -29,10 +29,11 @@ from zou.app.utils import cache, fs, logs from zou.app.utils.sentry import init_sentry -from zou.app.utils.user_agent import ParsedUserAgent +from zou.app.utils.flask import ParsedUserAgent, ORJSONProvider init_sentry() app = Flask(__name__) +app.json = ORJSONProvider(app) app.request_class.user_agent_class = ParsedUserAgent app.config.from_object(config) diff --git a/zou/app/utils/user_agent.py b/zou/app/utils/flask.py similarity index 57% rename from zou/app/utils/user_agent.py rename to zou/app/utils/flask.py index c4891c4bc0..6261d7d5e4 100644 --- a/zou/app/utils/user_agent.py +++ b/zou/app/utils/flask.py @@ -1,6 +1,23 @@ from ua_parser import user_agent_parser from werkzeug.user_agent import UserAgent from werkzeug.utils import cached_property +from flask.json.provider import JSONProvider +import orjson + + +class ORJSONProvider(JSONProvider): + def __init__(self, *args, **kwargs): + self.options = kwargs + super().__init__(*args, **kwargs) + + def loads(self, s, **kwargs): + return orjson.loads(s) + + def dumps(self, obj, **kwargs): + # decode back to str, as orjson returns bytes + return orjson.dumps(obj, option=orjson.OPT_NON_STR_KEYS).decode( + "utf-8" + ) class ParsedUserAgent(UserAgent): diff --git a/zou/event_stream.py b/zou/event_stream.py index a53e5e17a4..b0d7cc3edf 100644 --- a/zou/event_stream.py +++ b/zou/event_stream.py @@ -13,6 +13,7 @@ from zou.app import config from zou.app.stores import auth_tokens_store from zou.app.utils.sentry import init_sentry +from zou.app.utils.flask import ORJSONProvider server_stats = {"nb_connections": 0} rooms_data = {} @@ -216,6 +217,7 @@ def create_app(): ) init_sentry() app = Flask(__name__) + app.json = ORJSONProvider(app) app.config.from_object(config) set_info_routes(socketio, app) set_application_routes(socketio, app)