| Conditions | 5 |
| Total Lines | 26 |
| Code Lines | 15 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | from typing import Optional, Callable |
||
| 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 |