def _set_remaining_attrs(instance, remaining_attrs, **kwargs): # Set any remaining attributes on the newly created instance. for attr_name in remaining_attrs: loaded_attr = _common_impl.load(remaining_attrs[attr_name], type(remaining_attrs[attr_name]), **kwargs) try: setattr(instance, attr_name, loaded_attr) except AttributeError: pass # This is raised when a @property does not have a setter.
def default_list_deserializer(obj: List, cls, **kwargs) -> object: """ Deserialize a list by deserializing all items of that list. :param obj: the list that needs deserializing. :param cls: the type optionally with a generic (e.g. List[str]). :param kwargs: any keyword arguments. :return: a deserialized list instance. """ cls_ = None if cls and hasattr(cls, '__args__'): cls_ = cls.__args__[0] return [_common_impl.load(x, cls_, **kwargs) for x in obj]
def _get_constructor_args(obj, signature_parameters, **kwargs): # Loop through the signature of cls: the type we try to deserialize to. For # every required parameter, we try to get the corresponding value from # json_obj. constructor_args = dict() sigs = [(sig_key, sig) for sig_key, sig in signature_parameters.items() if obj and sig_key != 'self' and sig_key in obj] for sig_key, sig in sigs: cls = sig.annotation if sig.annotation != inspect.Parameter.empty \ else None value = _common_impl.load(obj[sig_key], cls, **kwargs) constructor_args[sig_key] = value return constructor_args
def default_string_deserializer(obj: str, _: type = None, **kwargs) -> object: """ Deserialize a string. If the given ``obj`` can be parsed to a date, a ``datetime`` instance is returned. :param obj: the string that is to be deserialized. :param _: not used. :param kwargs: any keyword arguments. :return: the deserialized obj. """ try: # Use load instead of default_datetime_deserializer to allow the # datetime deserializer to be overridden. return _common_impl.load(obj, datetime, **kwargs) except: return obj
def default_tuple_deserializer(obj: List, cls, **kwargs) -> object: """ Deserialize a (JSON) list into a tuple by deserializing all items of that list. :param obj: the list that needs deserializing. :param cls: the type optionally with a generic (e.g. Tuple[str, int]). :param kwargs: any keyword arguments. :return: a deserialized tuple instance. """ if hasattr(cls, '__tuple_params__'): tuple_types = cls.__tuple_params__ else: tuple_types = cls.__args__ list_ = [_common_impl.load(obj[i], tuple_types[i], **kwargs) for i in range(len(obj))] return tuple(list_)
def default_tuple_deserializer(obj: List, cls, **kwargs) -> object: """ Deserialize a (JSON) list into a tuple by deserializing all items of that list. :param obj: the list that needs deserializing. :param cls: the type optionally with a generic (e.g. Tuple[str, int]). :param kwargs: any keyword arguments. :return: a deserialized tuple instance. """ tuple_types = getattr(cls, '__tuple_params__', cls.__args__) if len(tuple_types) > 1 and tuple_types[1] is ...: tuple_types = [tuple_types[0]] * len(obj) list_ = [ _common_impl.load(value, tuple_types[i], **kwargs) for i, value in enumerate(obj) ] return tuple(list_)
def default_dict_deserializer(obj: dict, cls: type, key_transformer: Callable[[str], str] = None, **kwargs) -> object: """ Deserialize a dict by deserializing all instances of that dict. :param obj: the dict that needs deserializing. :param key_transformer: a function that transforms the keys to a different style (e.g. PascalCase). :param cls: not used. :param kwargs: any keyword arguments. :return: a deserialized dict instance. """ key_transformer = key_transformer or (lambda key: key) kwargs_ = {**{'key_transformer': key_transformer}, **kwargs} if hasattr(cls, '__args__') and len(cls.__args__) > 1: sub_cls = cls.__args__[1] kwargs_['cls'] = sub_cls return {key_transformer(key): _common_impl.load(obj[key], **kwargs_) for key in obj}