Total Complexity | 3 |
Total Lines | 31 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | from collections.abc import Mapping, Iterable |
||
2 | from typing import Iterable as IterableType |
||
3 | |||
4 | from jsons._compatibility_impl import get_naked_class |
||
5 | from jsons.deserializers.default_list import default_list_deserializer |
||
6 | |||
7 | |||
8 | def default_iterable_deserializer( |
||
9 | obj: list, |
||
10 | cls: type, |
||
11 | **kwargs) -> Iterable: |
||
12 | """ |
||
13 | Deserialize a (JSON) list into an ``Iterable`` by deserializing all items |
||
14 | of that list. The given obj is assumed to be homogeneous; if the list has a |
||
15 | generic type (e.g. Set[datetime]) then it is assumed that all elements can |
||
16 | be deserialized to that type. |
||
17 | :param obj: The list that needs deserializing to an ``Iterable``. |
||
18 | :param cls: The type, optionally with a generic (e.g. Deque[str]). |
||
19 | :param kwargs: Any keyword arguments. |
||
20 | :return: A deserialized ``Iterable`` (e.g. ``set``) instance. |
||
21 | """ |
||
22 | cls_ = Mapping |
||
23 | if hasattr(cls, '__args__'): |
||
24 | cls_ = IterableType[cls.__args__] |
||
25 | list_ = default_list_deserializer(obj, cls_, **kwargs) |
||
26 | result = list_ |
||
27 | naked_cls = get_naked_class(cls) |
||
28 | if not isinstance(result, naked_cls): |
||
29 | result = naked_cls(list_) |
||
30 | return result |
||
31 |