Skip to content

Commit 7f30c17

Browse files
committed
fix: fixed updated JSON handling in Flask 2.3+
1 parent a2fa352 commit 7f30c17

File tree

2 files changed

+13
-23
lines changed

2 files changed

+13
-23
lines changed

aw_server/rest.py

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from threading import Lock
66
from typing import Dict
77

8+
import flask.json.provider
89
import iso8601
910
from aw_core import schema
1011
from aw_core.models import Event
@@ -54,10 +55,7 @@ def decorator(*args, **kwargs):
5455

5556
# TODO: Clean up JSONEncoder code?
5657
# Move to server.py
57-
class CustomJSONEncoder(json.JSONEncoder):
58-
def __init__(self, *args, **kwargs):
59-
super().__init__()
60-
58+
class CustomJSONProvider(flask.json.provider.DefaultJSONProvider):
6159
def default(self, obj, *args, **kwargs):
6260
try:
6361
if isinstance(obj, datetime):
@@ -66,15 +64,7 @@ def default(self, obj, *args, **kwargs):
6664
return obj.total_seconds()
6765
except TypeError:
6866
pass
69-
return json.JSONEncoder.default(self, obj)
70-
71-
72-
class AnyJson(fields.Raw):
73-
def format(self, value):
74-
if type(value) == dict:
75-
return value
76-
else:
77-
return json.loads(value)
67+
return super().default(obj)
7868

7969

8070
# Loads event and bucket schema from JSONSchema in aw_core

aw_server/server.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@
2626

2727

2828
class AWFlask(Flask):
29-
def __init__(self, name, *args, **kwargs):
29+
def __init__(self, name, testing: bool, *args, **kwargs):
30+
self.json_provider_class = rest.CustomJSONProvider
31+
32+
# Only pretty-print JSON if in testing mode (because of performance)
33+
self.json_provider_class.compact = not testing
34+
35+
# Initialize Flask
3036
Flask.__init__(self, name, *args, **kwargs)
3137

3238
# Is set on later initialization
@@ -36,23 +42,17 @@ def __init__(self, name, *args, **kwargs):
3642
def create_app(
3743
host: str, testing=True, storage_method=None, cors_origins=[], custom_static=dict()
3844
) -> AWFlask:
39-
app = AWFlask("aw-server", static_folder=static_folder, static_url_path="")
40-
41-
if storage_method is None:
42-
storage_method = aw_datastore.get_storage_methods()["memory"]
43-
44-
# Only pretty-print JSON if in testing mode (because of performance)
45-
app.config["JSONIFY_PRETTYPRINT_REGULAR"] = testing
45+
app = AWFlask("aw-server", testing, static_folder=static_folder, static_url_path="")
4646

4747
with app.app_context():
4848
_config_cors(cors_origins, testing)
4949

50-
app.json_encoder = rest.CustomJSONEncoder
51-
5250
app.register_blueprint(root)
5351
app.register_blueprint(rest.blueprint)
5452
app.register_blueprint(get_custom_static_blueprint(custom_static))
5553

54+
if storage_method is None:
55+
storage_method = aw_datastore.get_storage_methods()["memory"]
5656
db = Datastore(storage_method, testing=testing)
5757
app.api = ServerAPI(db=db, testing=testing)
5858

0 commit comments

Comments
 (0)