diff options
| author | 2013-03-11 19:49:20 +0100 | |
|---|---|---|
| committer | 2013-03-11 19:50:54 +0100 | |
| commit | 763b142db70ce77952cb46cfccf84d9800f15651 (patch) | |
| tree | d2636e48766d365bd8a9d079de603b127ab88744 /module/remote/json_converter.py | |
| parent | Merge pull request #33 from stickell/patch-3 (diff) | |
| download | pyload-763b142db70ce77952cb46cfccf84d9800f15651.tar.xz | |
websocket login via session, websocket pushes server status, webui renders server status
Diffstat (limited to 'module/remote/json_converter.py')
| -rw-r--r-- | module/remote/json_converter.py | 22 | 
1 files changed, 20 insertions, 2 deletions
| 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) | 
