From 36f0946e2d7ccc7b9736eaac5ebd4852d070eab7 Mon Sep 17 00:00:00 2001 From: Lex Neva Date: Wed, 22 Feb 2023 21:08:40 -0500 Subject: [PATCH] fix flask json deprecation --- lib/api/server.py | 4 ++-- lib/utils/json.py | 12 +++++++++--- requirements.txt | 2 +- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/api/server.py b/lib/api/server.py index 626e412e6..f9bb64586 100644 --- a/lib/api/server.py +++ b/lib/api/server.py @@ -14,7 +14,7 @@ import requests from flask import Flask, g from werkzeug.serving import make_server -from ..utils.json import InkStitchJSONEncoder +from ..utils.json import InkStitchJSONProvider from .install import install from .simulator import simulator from .stitch_plan import stitch_plan @@ -41,7 +41,7 @@ class APIServer(Thread): cli.show_server_banner = lambda *x: None self.app = Flask(__name__) - self.app.json_encoder = InkStitchJSONEncoder + self.app.json = InkStitchJSONProvider(self.app) self.app.register_blueprint(simulator, url_prefix="/simulator") self.app.register_blueprint(stitch_plan, url_prefix="/stitch_plan") diff --git a/lib/utils/json.py b/lib/utils/json.py index 63b3cfb50..22107c489 100644 --- a/lib/utils/json.py +++ b/lib/utils/json.py @@ -3,10 +3,15 @@ # Copyright (c) 2010 Authors # Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details. -from flask.json import JSONEncoder +from flask.json.provider import DefaultJSONProvider +from ..exceptions import InkstitchException -class InkStitchJSONEncoder(JSONEncoder): +class InkstitchJSONException(InkstitchException): + pass + + +class InkStitchJSONProvider(DefaultJSONProvider): """JSON encoder class that runs __json__ on an object if available. The __json__ method should return a JSON-compatible representation of the @@ -17,4 +22,5 @@ class InkStitchJSONEncoder(JSONEncoder): try: return obj.__json__() except AttributeError: - return JSONEncoder.default(self, obj) + raise InkstitchJSONException( + f"Object of type {obj.__class__.__name__} cannot be serialized to JSON because it does not implement __json__()") diff --git a/requirements.txt b/requirements.txt index dd4a492aa..05908f8d2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -22,7 +22,7 @@ colormath @ git+https://github.com/gtaylor/python-colormath.git@4a076831fd5136f6 stringcase tinycss2 -flask +flask>=2.2.0 fonttools trimesh>=3.15.2 scipy