def marshal()

in mozetl/taar/taar_amodump.py [0:0]


def marshal(value, name, type_def):
    serializers = {
        typing.List: list,
        typing.Dict: dict,
        str: text_type,
        text_type: text_type,
        int: int,
        float: float,
        bool: bool,
    }

    if issubclass(type_def, JSONSchema):
        obj = {}
        for attr_name, attr_type_def in list(type_def.meta.items()):
            attr_value = value.get(attr_name, Undefined)
            if attr_value is not Undefined:
                # Try marshalling the value
                obj[attr_name] = marshal(attr_value, attr_name, attr_type_def)
        return obj
    elif issubclass(type_def, typing.Container) and type_def not in [str, bytes]:
        if issubclass(type_def, typing.List):
            item_type = type_def.__args__[0]
            return [marshal(j, name, item_type) for j in value]
        elif issubclass(type_def, typing.Dict):
            if value is None:
                return None
            k_cast, v_cast = type_def.__args__
            dict_vals = [
                (marshal(k, name, k_cast), marshal(v, name, v_cast))
                for k, v in list(value.items())
            ]
            return dict(dict_vals)
    else:
        return serializers[type_def](value)