Passed
Pull Request — master (#127)
by Ramon
01:06
created

default_union_serializer()   B

Complexity

Conditions 6

Size

Total Lines 29
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 29
rs 8.6666
c 0
b 0
f 0
cc 6
nop 3
1
from typing import Union
2
from jsons._common_impl import get_class_name, NoneType
3
from jsons._compatibility_impl import get_union_params
4
from jsons._dump_impl import dump
5
from jsons.exceptions import JsonsError, SerializationError
6
7
8
def default_union_serializer(obj: object, cls: Union, **kwargs) -> object:
9
    """
10
    Serialize an object to any matching type of the given union. The first
11
    successful serialization is returned.
12
    :param obj: The object that is to be serialized.
13
    :param cls: The Union type with a generic (e.g. Union[str, int]).
14
    :param kwargs: Any keyword arguments that are passed through the
15
    serialization process.
16
    :return: An object of the first type of the Union that could be
17
    serialized successfully.
18
    """
19
    sub_types = get_union_params(cls)
20
21
    # Cater for Optional[...]/Union[None, ...] first to avoid blindly
22
    # string-ifying None in later serializers.
23
    if obj is None and NoneType in sub_types:
24
        return obj
25
26
    for sub_type in sub_types:
27
        try:
28
            return dump(obj, sub_type, **kwargs)
29
        except JsonsError:
30
            pass  # Try the next one.
31
    else:
32
        args_msg = ', '.join([get_class_name(cls_)
33
                              for cls_ in get_union_params(cls)])
34
        err_msg = ('Could not match the object of type "{}" to any type of '
35
                   'the Union: {}'.format(str(cls), args_msg))
36
        raise SerializationError(err_msg)
37