How to use the jsons.exceptions.DeserializationError function in jsons

To help you get started, we’ve selected a few jsons examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ramonhagenaars / jsons / jsons / deserializers.py View on Github external
:param cls: The Union type with a generic (e.g. Union[str, int]).
    :param kwargs: Any keyword arguments that are passed through the
    deserialization process.
    :return: An object of the first type of the Union that could be
    deserialized successfully.
    """
    for sub_type in cls.__args__:
        try:
            return _main_impl.load(obj, sub_type, **kwargs)
        except JsonsError:
            pass  # Try the next one.
    else:
        args_msg = ', '.join([get_class_name(cls_) for cls_ in cls.__args__])
        err_msg = ('Could not match the object of type "{}" to any type of '
                   'the Union: {}'.format(str(cls), args_msg))
        raise DeserializationError(err_msg, obj, cls)
github ramonhagenaars / jsons / jsons / _load_impl.py View on Github external
def _do_load(json_obj: object,
             deserializer: callable,
             cls: type,
             initial: bool,
             **kwargs):
    try:
        result = deserializer(json_obj, cls, **kwargs)
        validate(result, cls, kwargs['fork_inst'])
    except Exception as err:
        clear()
        if isinstance(err, JsonsError):
            raise
        raise DeserializationError(str(err), json_obj, cls)
    else:
        if initial:
            # Clear all lru caches right before returning the initial call.
            clear()
        return result
github ramonhagenaars / jsons / jsons / _main_impl.py View on Github external
json_obj, cls, fork_inst, kwargs.get('_inferred_cls', False))

    deserializer = _get_deserializer(cls, fork_inst)
    kwargs_ = {
        'strict': strict,
        'fork_inst': fork_inst,
        'attr_getters': attr_getters,
        'meta_hints': meta_hints,
        **kwargs
    }
    try:
        return deserializer(json_obj, cls, **kwargs_)
    except Exception as err:
        if isinstance(err, JsonsError):
            raise
        raise DeserializationError(str(err), json_obj, cls)
github ramonhagenaars / jsons / jsons / exceptions.py View on Github external
def __init__(self,
                 message: str,
                 argument: str,
                 source: object,
                 target: type):
        """
        Constructor.
        :param message: the message of this error.
        :param argument: the argument that was unfulfilled.
        :param source: the object that was to be deserialized.
        :param target: the type to which `source` was to be deserialized.
        """
        DeserializationError.__init__(self, message, source, target)
        ArgumentError.__init__(self, message, argument)
github ramonhagenaars / jsons / jsons / _main_impl.py View on Github external
json_obj: object,
        cls: type,
        fork_inst: type,
        inferred_cls: bool) -> Tuple[type, Optional[dict]]:
    # Check if json_obj is of a valid type and return the cls.
    if type(json_obj) not in VALID_TYPES:
        invalid_type = get_class_name(type(json_obj), fork_inst=fork_inst,
                                      fully_qualified=True)
        valid_types = [get_class_name(typ, fork_inst=fork_inst,
                                      fully_qualified=True)
                       for typ in VALID_TYPES]
        msg = ('Invalid type: "{}", only arguments of the following types are '
               'allowed: {}'.format(invalid_type, ", ".join(valid_types)))
        raise DeserializationError(msg, json_obj, cls)
    if json_obj is None:
        raise DeserializationError('Cannot load None with strict=True',
                                   json_obj, cls)

    cls_from_meta, meta = get_cls_and_meta(json_obj, fork_inst)
    meta_hints = meta.get('classes', {}) if meta else {}
    return determine_precedence(
        cls, cls_from_meta, type(json_obj), inferred_cls), meta_hints
github ramonhagenaars / jsons / jsons / exceptions.py View on Github external
message: str,
                 argument: str,
                 source: object,
                 target: type):
        """
        Constructor.
        :param message: the message of this error.
        :param argument: the argument that was unfulfilled.
        :param source: the object that was to be deserialized.
        :param target: the type to which `source` was to be deserialized.
        """
        DeserializationError.__init__(self, message, source, target)
        ArgumentError.__init__(self, message, argument)


class SignatureMismatchError(DeserializationError, ArgumentError):
    """
    Raised when the source could not be deserialized into the target type due
    to a mismatch between the source's attributes and the target's accepted
    parameters. This error is raised in "strict-mode" only.
    """
    def __init__(self,
                 message: str,
                 argument: str,
                 source: object,
                 target: type):
        """
        Constructor.
        :param message: the message of this error.
        :param argument: the argument that caused the problem.
        :param source: the object that was to be deserialized.
        :param target: the type to which `source` was to be deserialized.
github ramonhagenaars / jsons / jsons / deserializers / default_nonetype.py View on Github external
def default_nonetype_deserializer(obj: object,
                                  cls: Optional[type] = None,
                                  **kwargs) -> object:
    """
    Deserialize a ``NoneType``.
    :param obj: the value that is to be deserialized.
    :param cls: not used.
    :param kwargs: not used.
    :return: ``obj``.
    """
    if obj is not None:
        raise DeserializationError('Cannot deserialize {} as NoneType'
                                   .format(obj), source=obj, target=cls)
    return obj
github ramonhagenaars / jsons / jsons / _main_impl.py View on Github external
def _check_and_get_cls_and_meta_hints(
        json_obj: object,
        cls: type,
        fork_inst: type,
        inferred_cls: bool) -> Tuple[type, Optional[dict]]:
    # Check if json_obj is of a valid type and return the cls.
    if type(json_obj) not in VALID_TYPES:
        invalid_type = get_class_name(type(json_obj), fork_inst=fork_inst,
                                      fully_qualified=True)
        valid_types = [get_class_name(typ, fork_inst=fork_inst,
                                      fully_qualified=True)
                       for typ in VALID_TYPES]
        msg = ('Invalid type: "{}", only arguments of the following types are '
               'allowed: {}'.format(invalid_type, ", ".join(valid_types)))
        raise DeserializationError(msg, json_obj, cls)
    if json_obj is None:
        raise DeserializationError('Cannot load None with strict=True',
                                   json_obj, cls)

    cls_from_meta, meta = get_cls_and_meta(json_obj, fork_inst)
    meta_hints = meta.get('classes', {}) if meta else {}
    return determine_precedence(
        cls, cls_from_meta, type(json_obj), inferred_cls), meta_hints
github ramonhagenaars / jsons / jsons / _main_impl.py View on Github external
Extend ``json.loads``, allowing bytes to be loaded into a dict or a Python
    instance of type ``cls``. Any extra (keyword) arguments are passed on to
    ``json.loads``.

    :param bytes_: the bytes that are to be loaded.
    :param cls: a matching class of which an instance should be returned.
    :param encoding: the encoding that is used to transform from bytes.
    :param jdkwargs: extra keyword arguments for ``json.loads`` (not
    ``jsons.loads``!)
    :param args: extra arguments for ``jsons.loads``.
    :param kwargs: extra keyword arguments for ``jsons.loads``.
    :return: a JSON-type object (dict, str, list, etc.) or an instance of type
    ``cls`` if given.
    """
    if not isinstance(bytes_, bytes):
        raise DeserializationError('loadb accepts bytes only, "{}" was given'
                                   .format(type(bytes_)), bytes_, cls)
    jdkwargs = jdkwargs or {}
    str_ = bytes_.decode(encoding=encoding)
    return loads(str_, cls, jdkwargs=jdkwargs, *args, **kwargs)