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

jsons.serializers.default_dict   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 18
dl 0
loc 31
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A default_dict_serializer() 0 26 5
1
from typing import Optional, Callable
2
from jsons._main_impl import dump
3
4
5
def default_dict_serializer(
6
        obj: dict,
7
        cls: Optional[type] = None,
8
        strip_nulls: bool = False,
9
        key_transformer: Optional[Callable[[str], str]] = None,
10
        **kwargs) -> dict:
11
    """
12
    Serialize the given ``obj`` to a dict of serialized objects.
13
    :param obj: the dict that is to be serialized.
14
    :param key_transformer: a function that will be applied to all keys in the
15
    resulting dict.
16
    :param strip_nulls: if ``True`` the resulting dict will not contain null
17
    values.
18
    :param kwargs: any keyword arguments that may be given to the serialization
19
    process.
20
    :return: a dict of which all elements are serialized.
21
    """
22
    result = dict()
23
    for key in obj:
24
        dumped_elem = dump(obj[key], key_transformer=key_transformer,
25
                           strip_nulls=strip_nulls, **kwargs)
26
        if not (strip_nulls and dumped_elem is None):
27
            if key_transformer:
28
                key = key_transformer(key)
29
            result[key] = dumped_elem
30
    return result
31