diff options
Diffstat (limited to 'module/remote/json_converter.py')
-rw-r--r-- | module/remote/json_converter.py | 64 |
1 files changed, 0 insertions, 64 deletions
diff --git a/module/remote/json_converter.py b/module/remote/json_converter.py deleted file mode 100644 index 50f0309bd..000000000 --- a/module/remote/json_converter.py +++ /dev/null @@ -1,64 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- - -try: - from module.common.json_layer import json -except ImportError: - import json - - -import apitypes -from apitypes import BaseObject -from apitypes import ExceptionObject - -# compact json separator -separators = (',', ':') - -# json encoder that accepts api objects -class BaseEncoder(json.JSONEncoder): - - def default(self, o): - if isinstance(o, BaseObject) or isinstance(o, ExceptionObject): - ret = {"@class" : o.__class__.__name__} - for att in o.__slots__: - ret[att] = getattr(o, att) - return ret - - 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): - if 'compact' in kwargs: - kwargs['cls'] = BaseEncoderCompact - del kwargs['compact'] - else: - kwargs['cls'] = BaseEncoder - - kwargs['separators'] = separators - return json.dumps(*args, **kwargs) - - -def loads(*args, **kwargs): - kwargs['object_hook'] = convert_obj - return json.loads(*args, **kwargs)
\ No newline at end of file |