Consolidate json encoding like in 1.3.x
This commit is contained in:
1 parent
d1711f0376
commit
cedbf039d2
5 files changed
+20
-72
No files matched your search
@@ -21,6 +21,7 @@ from builtins import bytes, range
|
||||
from past.builtins import basestring
|
||||
|
||||
from octoprint import util
|
||||
from octoprint.util.json import JsonEncoding
|
||||
|
||||
import os
|
||||
import logging
|
||||
@@ -95,7 +96,6 @@ import octoprint.slicing
|
||||
from octoprint.server.util import enforceApiKeyRequestHandler, loginFromApiKeyRequestHandler, corsRequestHandler, \
|
||||
corsResponseHandler
|
||||
from octoprint.server.util.flask import PreemptiveCache
|
||||
from octoprint.server.util.serialization import OctoPrintJsonEncoder, OctoPrintJsonDecoder
|
||||
|
||||
UI_API_KEY = ''.join('%02X' % z for z in bytes(uuid.uuid4().bytes))
|
||||
|
||||
@@ -285,12 +285,9 @@ class Server(object):
|
||||
pluginLifecycleManager = LifecycleManager(pluginManager)
|
||||
preemptiveCache = PreemptiveCache(os.path.join(self._settings.getBaseFolder("data"), "preemptive_cache_config.yaml"))
|
||||
|
||||
jsonEncoder = OctoPrintJsonEncoder()
|
||||
jsonDecoder = OctoPrintJsonDecoder()
|
||||
|
||||
jsonEncoder.add_multi_encoder(users.User, lambda u: u.as_dict())
|
||||
jsonEncoder.add_encoder(groups.Group, lambda g: g.as_dict())
|
||||
jsonEncoder.add_encoder(permissions.OctoPrintPermission, lambda op: op.as_dict())
|
||||
JsonEncoding.add_encoder(users.User, lambda obj: obj.as_dict())
|
||||
JsonEncoding.add_encoder(groups.Group, lambda obj: obj.as_dict())
|
||||
JsonEncoding.add_encoder(permissions.OctoPrintPermission, lambda obj: obj.as_dict())
|
||||
|
||||
# start regular check if we are connected to the internet
|
||||
connectivityEnabled = self._settings.getBoolean(["server", "onlineCheck", "enabled"])
|
||||
@@ -845,13 +842,13 @@ class Server(object):
|
||||
timer.start()
|
||||
|
||||
def _setup_app(self, app):
|
||||
from octoprint.server.util.flask import ReverseProxiedEnvironment, OctoPrintFlaskRequest, OctoPrintFlaskResponse
|
||||
from octoprint.server.util.flask import ReverseProxiedEnvironment, OctoPrintFlaskRequest, \
|
||||
OctoPrintFlaskResponse, OctoPrintJsonEncoder
|
||||
|
||||
s = settings()
|
||||
|
||||
# setup octoprint's flask json serialization/deserialization
|
||||
app.json_encoder = OctoPrintJsonEncoder.Encoder
|
||||
app.json_decoder = OctoPrintJsonDecoder.Decoder
|
||||
app.json_encoder = OctoPrintJsonEncoder
|
||||
|
||||
app.debug = self._debug
|
||||
app.config["TEMPLATES_AUTO_RELOAD"] = True
|
||||
|
||||
@@ -24,7 +24,6 @@ from . import flask
|
||||
from . import sockjs
|
||||
from . import tornado
|
||||
from . import watchdog
|
||||
from . import serialization
|
||||
|
||||
|
||||
def enforceApiKeyRequestHandler():
|
||||
|
||||
@@ -1417,3 +1417,9 @@ def collect_plugin_assets(enable_gcodeviewer=True, preferred_stylesheet="css"):
|
||||
break
|
||||
|
||||
return assets
|
||||
|
||||
##~~ JSON encoding
|
||||
|
||||
class OctoPrintJsonEncoder(flask.json.JSONEncoder):
|
||||
def default(self, obj):
|
||||
return JsonEncoding.encode(obj)
|
||||
@@ -1,61 +0,0 @@
|
||||
# coding=utf-8
|
||||
from __future__ import absolute_import, division, print_function
|
||||
|
||||
__author__ = "Marc Hannappel <salandora@gmail.com>"
|
||||
__license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
|
||||
__copyright__ = "Copyright (C) 2014 The OctoPrint Project - Released under terms of the AGPLv3 License"
|
||||
|
||||
from flask.json import JSONEncoder, JSONDecoder
|
||||
|
||||
class OctoPrintJsonEncoder:
|
||||
json_encoder = dict()
|
||||
json_multi_encoder = dict()
|
||||
|
||||
class Encoder(JSONEncoder):
|
||||
def default(self, obj):
|
||||
for data_type, func in OctoPrintJsonEncoder.json_encoder.items():
|
||||
if isinstance(obj, data_type):
|
||||
node = func(obj)
|
||||
break
|
||||
else:
|
||||
for data_type, func in OctoPrintJsonEncoder.json_multi_encoder.items():
|
||||
if isinstance(obj, data_type):
|
||||
node = func(obj)
|
||||
break
|
||||
else:
|
||||
node = JSONEncoder.default(self, obj)
|
||||
|
||||
return node
|
||||
|
||||
def add_encoder(self, data_type, encoder):
|
||||
OctoPrintJsonEncoder.json_encoder[data_type] = encoder
|
||||
|
||||
def add_multi_encoder(self, data_type, encoder):
|
||||
OctoPrintJsonEncoder.json_multi_encoder[data_type] = encoder
|
||||
|
||||
|
||||
class OctoPrintJsonDecoder:
|
||||
json_decoder = dict()
|
||||
|
||||
class Decoder(JSONDecoder):
|
||||
def __init__(self, **kwargs):
|
||||
JSONDecoder.__init__(self, object_hook=self.dict_to_object)
|
||||
self._object_hook = kwargs.pop('object_hook', None)
|
||||
|
||||
def dict_to_object(self, d):
|
||||
inst = None
|
||||
if self._object_hook is not None:
|
||||
inst = self._object_hook(d)
|
||||
|
||||
if inst is None:
|
||||
for decoder in OctoPrintJsonDecoder.json_decoder:
|
||||
inst = OctoPrintJsonDecoder.json_decoder[decoder](self, d)
|
||||
if inst is not None:
|
||||
return inst
|
||||
else:
|
||||
inst = d
|
||||
|
||||
return inst
|
||||
|
||||
def add_decoder(self, name, decoder):
|
||||
OctoPrintJsonDecoder.json_decoder[name] = decoder
|
||||
@@ -16,6 +16,13 @@ class JsonEncoding(object):
|
||||
def add_encoder(cls, type, encoder):
|
||||
cls.encoders[type] = encoder
|
||||
|
||||
@classmethod
|
||||
def remove_encoder(cls, type):
|
||||
try:
|
||||
del cls.encoders[type]
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
@classmethod
|
||||
def encode(cls, obj):
|
||||
for type, encoder in cls.encoders.items():
|
||||
|
||||
Reference in new issue
Block a user