Skip to content

Commit 470b4f6

Browse files
author
Joel Collins
committed
Let LabThing object handle choice of JSON encoder
1 parent 58d9318 commit 470b4f6

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

src/labthings/labthing.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,29 +51,32 @@ def __init__(
5151
types: list = None,
5252
version: str = "0.0.0",
5353
format_flask_exceptions: bool = True,
54+
json_encoder=LabThingsJSONEncoder,
5455
):
5556
if types is None:
5657
types = []
5758
self.app = app # Becomes a Flask app
5859
self.sockets = None # Becomes a Socket(app) websocket handler
5960

60-
self.components = {}
61+
self.components = (
62+
{}
63+
) # Dictionary of attached component objects, available to extensions
6164

62-
self.extensions = {}
65+
self.extensions = {} # Dictionary of LabThings extension objects
6366

6467
self.actions = Pool() # Pool of threads for Actions
6568

66-
self.events = {}
69+
self.events = {} # Dictionary of Event affordances
6770

68-
self.views = []
69-
self._property_views = {}
70-
self._action_views = {}
71+
self.views = [] # List of View classes
72+
self._property_views = {} # Dictionary of PropertyView views
73+
self._action_views = {} # Dictionary of ActionView views
7174

72-
self.subscribers = set()
75+
self.subscribers = set() # Set of connected event subscribers
7376

74-
self.endpoints = set()
77+
self.endpoints = set() # Set of endpoint strings
7578

76-
self.url_prefix = prefix
79+
self.url_prefix = prefix # Global URL prefix for all LabThings views
7780

7881
for t in types:
7982
if ";" in t:
@@ -97,7 +100,7 @@ def __init__(
97100
# Representation formatter map
98101
self.representations = DEFAULT_REPRESENTATIONS
99102

100-
# API Spec
103+
# OpenAPI spec for Swagger docs
101104
self.spec = APISpec(
102105
title=self.title,
103106
version=self.version,
@@ -108,6 +111,9 @@ def __init__(
108111
# Thing description
109112
self.thing_description = ThingDescription()
110113

114+
# JSON encoder class
115+
self.json_encoder = json_encoder
116+
111117
if app is not None:
112118
self.init_app(app)
113119

@@ -162,7 +168,7 @@ def init_app(self, app):
162168
error_handler.init_app(app)
163169

164170
# Custom JSON encoder
165-
app.json_encoder = LabThingsJSONEncoder
171+
app.json_encoder = self.json_encoder
166172

167173
# Add resources, if registered before tying to a Flask app
168174
if len(self.views) > 0:

src/labthings/representations.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@
22
from collections import OrderedDict
33

44
from .json.encoder import LabThingsJSONEncoder, encode_json
5+
from .json.encoder import JSONEncoder as FlaskJSONEncoder
6+
from .find import current_labthing
57
from .utilities import PY3
68

79

810
def output_json(data, code, headers=None):
911
"""Makes a Flask response with a JSON encoded body, using app JSON settings"""
1012

1113
settings = current_app.config.get("LABTHINGS_JSON", {})
12-
encoder = (
13-
current_app.config.get("LABTHINGS_JSON_ENCODER", {}) or LabThingsJSONEncoder
14-
)
14+
15+
if current_labthing():
16+
encoder = current_labthing().json_encoder
17+
else:
18+
encoder = getattr(current_app, "json_encoder", None) or FlaskJSONEncoder
1519

1620
if current_app.debug:
1721
settings.setdefault("indent", 4)

0 commit comments

Comments
 (0)