Passed
Pull Request — master (#17)
by Ramon
50s
created

default_dict_deserializer()   A

Complexity

Conditions 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 21
rs 9.8
c 0
b 0
f 0
cc 4
nop 4
1
from typing import Optional, Callable
2
from jsons._main_impl import load
3
4
5
def default_dict_deserializer(
6
        obj: dict,
7
        cls: type,
8
        key_transformer: Optional[Callable[[str], str]] = None,
9
        **kwargs) -> dict:
10
    """
11
    Deserialize a dict by deserializing all instances of that dict.
12
    :param obj: the dict that needs deserializing.
13
    :param key_transformer: a function that transforms the keys to a different
14
    style (e.g. PascalCase).
15
    :param cls: not used.
16
    :param kwargs: any keyword arguments.
17
    :return: a deserialized dict instance.
18
    """
19
    key_transformer = key_transformer or (lambda key: key)
20
    kwargs_ = {**{'key_transformer': key_transformer}, **kwargs}
21
    if hasattr(cls, '__args__') and len(cls.__args__) > 1:
22
        sub_cls = cls.__args__[1]
23
        kwargs_['cls'] = sub_cls
24
    return {key_transformer(key): load(obj[key], **kwargs_)
25
            for key in obj}
26