diff options
Diffstat (limited to 'jsonrpclib/jsonclass.py')
-rw-r--r-- | jsonrpclib/jsonclass.py | 31 |
1 files changed, 17 insertions, 14 deletions
diff --git a/jsonrpclib/jsonclass.py b/jsonrpclib/jsonclass.py index 1d86d5f..4326f28 100644 --- a/jsonrpclib/jsonclass.py +++ b/jsonrpclib/jsonclass.py @@ -1,7 +1,6 @@ import types import inspect import re -import traceback from jsonrpclib import config @@ -30,9 +29,11 @@ value_types = [ supported_types = iter_types+string_types+numeric_types+value_types invalid_module_chars = r'[^a-zA-Z0-9\_\.]' + class TranslationError(Exception): pass + def dump(obj, serialize_method=None, ignore_attribute=None, ignore=[]): if not serialize_method: serialize_method = config.serialize_method @@ -46,17 +47,17 @@ def dump(obj, serialize_method=None, ignore_attribute=None, ignore=[]): if obj_type in (types.ListType, types.TupleType): new_obj = [] for item in obj: - new_obj.append(dump(item, serialize_method, - ignore_attribute, ignore)) - if obj_type is types.TupleType: + new_obj.append( + dump(item, serialize_method, ignore_attribute, ignore)) + if isinstance(obj_type, types.TupleType): new_obj = tuple(new_obj) return new_obj # It's a dict... else: new_obj = {} for key, value in obj.iteritems(): - new_obj[key] = dump(value, serialize_method, - ignore_attribute, ignore) + new_obj[key] = dump( + value, serialize_method, ignore_attribute, ignore) return new_obj # It's not a standard type, so it needs __jsonclass__ module_name = inspect.getmodule(obj).__name__ @@ -64,7 +65,7 @@ def dump(obj, serialize_method=None, ignore_attribute=None, ignore=[]): json_class = class_name if module_name not in ['', '__main__']: json_class = '%s.%s' % (module_name, json_class) - return_obj = {"__jsonclass__":[json_class,]} + return_obj = {"__jsonclass__": [json_class]} # If a serialization method is defined.. if serialize_method in dir(obj): # Params can be a dict (keyword) or list (positional) @@ -84,21 +85,23 @@ def dump(obj, serialize_method=None, ignore_attribute=None, ignore=[]): if type(attr_value) in supported_types and \ attr_name not in ignore_list and \ attr_value not in ignore_list: - attrs[attr_name] = dump(attr_value, serialize_method, - ignore_attribute, ignore) + attrs[attr_name] = dump( + attr_value, serialize_method, ignore_attribute, ignore) return_obj.update(attrs) return return_obj + def load(obj): - if type(obj) in string_types+numeric_types+value_types: + if type(obj) in string_types + numeric_types + value_types: return obj - if type(obj) is types.ListType: + + if isinstance(obj, list): return_list = [] for entry in obj: return_list.append(load(entry)) return return_list # Othewise, it's a dict type - if '__jsonclass__' not in obj.keys(): + if '__jsonclass__' not in obj: return_dict = {} for key, value in obj.iteritems(): new_value = load(value) @@ -139,9 +142,9 @@ def load(obj): json_class = getattr(temp_module, json_class_name) # Creating the object... new_obj = None - if type(params) is types.ListType: + if isinstance(params, list): new_obj = json_class(*params) - elif type(params) is types.DictType: + elif isinstance(params, dict): new_obj = json_class(**params) else: raise TranslationError('Constructor args must be a dict or list.') |