summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLex Neva <github.com@lexneva.name>2023-02-22 21:08:40 -0500
committerLex Neva <github.com@lexneva.name>2023-02-22 21:10:33 -0500
commit36f0946e2d7ccc7b9736eaac5ebd4852d070eab7 (patch)
tree2957b542afe4b80cddcb892ef09f060fd1677f0a
parent7d57a22f01bebf43a9787d26543e3e5e029a8b9e (diff)
fix flask json deprecation
-rw-r--r--lib/api/server.py4
-rw-r--r--lib/utils/json.py12
-rw-r--r--requirements.txt2
3 files changed, 12 insertions, 6 deletions
diff --git a/lib/api/server.py b/lib/api/server.py
index 626e412e..f9bb6458 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 63b3cfb5..22107c48 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 dd4a492a..05908f8d 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