Total Complexity | 3 |
Total Lines | 28 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | from collections.abc import Mapping |
||
2 | from typing import Mapping as MappingType |
||
3 | |||
4 | from typish import get_args, get_origin |
||
5 | |||
6 | from jsons.deserializers.default_dict import default_dict_deserializer |
||
7 | |||
8 | |||
9 | def default_mapping_deserializer(obj: dict, cls: type, **kwargs) -> Mapping: |
||
10 | """ |
||
11 | Deserialize a (JSON) dict into a mapping by deserializing all items of that |
||
12 | dict. |
||
13 | :param obj: the dict that needs deserializing. |
||
14 | :param cls: the type, optionally with a generic (e.g. Set[str]). |
||
15 | :param kwargs: any keyword arguments. |
||
16 | :return: a deserialized set instance. |
||
17 | """ |
||
18 | cls_ = Mapping |
||
19 | cls_args = get_args(cls) |
||
20 | if cls_args: |
||
21 | cls_ = MappingType[cls_args] |
||
22 | dict_ = default_dict_deserializer(obj, cls_, **kwargs) |
||
23 | result = dict_ |
||
24 | # Strip any generics from cls to allow for an instance check. |
||
25 | if not isinstance(result, get_origin(cls)): |
||
26 | result = cls(dict_) |
||
27 | return result |
||
28 |