jsons.deserializers.default_string   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 17
dl 0
loc 28
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A default_string_deserializer() 0 19 3
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