Total Complexity | 3 |
Total Lines | 28 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | from datetime import datetime |
||
2 | from typing import Optional |
||
3 | |||
4 | from jsons._load_impl import load |
||
5 | from jsons.deserializers.default_primitive import default_primitive_deserializer |
||
6 | from jsons.exceptions import DeserializationError |
||
7 | |||
8 | |||
9 | def default_string_deserializer(obj: str, |
||
10 | cls: Optional[type] = None, |
||
11 | **kwargs) -> object: |
||
12 | """ |
||
13 | Deserialize a string. If the given ``obj`` can be parsed to a date, a |
||
14 | ``datetime`` instance is returned. |
||
15 | :param obj: the string that is to be deserialized. |
||
16 | :param cls: not used. |
||
17 | :param kwargs: any keyword arguments. |
||
18 | :return: the deserialized obj. |
||
19 | """ |
||
20 | target_is_str = cls is str and not kwargs.get('_inferred_cls') |
||
21 | if target_is_str: |
||
22 | return str(obj) |
||
23 | try: |
||
24 | result = load(obj, datetime, **kwargs) |
||
25 | except DeserializationError: |
||
26 | result = default_primitive_deserializer(obj, str) |
||
27 | return result |
||
28 |