From 763b142db70ce77952cb46cfccf84d9800f15651 Mon Sep 17 00:00:00 2001 From: RaNaN Date: Mon, 11 Mar 2013 19:49:20 +0100 Subject: websocket login via session, websocket pushes server status, webui renders server status --- module/remote/json_converter.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'module/remote/json_converter.py') diff --git a/module/remote/json_converter.py b/module/remote/json_converter.py index 256674c34..50f0309bd 100644 --- a/module/remote/json_converter.py +++ b/module/remote/json_converter.py @@ -14,7 +14,7 @@ from apitypes import ExceptionObject # compact json separator separators = (',', ':') -# json encoder that accepts TBase objects +# json encoder that accepts api objects class BaseEncoder(json.JSONEncoder): def default(self, o): @@ -26,17 +26,35 @@ class BaseEncoder(json.JSONEncoder): return json.JSONEncoder.default(self, o) +# more compact representation, only clients with information of the classes can handle it +class BaseEncoderCompact(json.JSONEncoder): + + def default(self, o): + if isinstance(o, BaseObject) or isinstance(o, ExceptionObject): + ret = {"@compact" : [o.__class__.__name__]} + ret["@compact"].extend(getattr(o, attr) for attr in o.__slots__) + return ret + + return json.JSONEncoder.default(self, o) def convert_obj(dct): if '@class' in dct: cls = getattr(apitypes, dct['@class']) del dct['@class'] return cls(**dct) + elif '@compact' in dct: + cls = getattr(apitypes, dct['@compact'][0]) + return cls(*dct['@compact'][1:]) return dct def dumps(*args, **kwargs): - kwargs['cls'] = BaseEncoder + if 'compact' in kwargs: + kwargs['cls'] = BaseEncoderCompact + del kwargs['compact'] + else: + kwargs['cls'] = BaseEncoder + kwargs['separators'] = separators return json.dumps(*args, **kwargs) -- cgit v1.2.3