JSON Voorhees
Killer JSON for C++
Loading...
Searching...
No Matches
serialization_error.hpp
Go to the documentation of this file.
1/// \file jsonv/serialization/serialization_error.hpp
2/// The exception a failed serialization throws. A header of its own, so that \c formats.hpp can derive
3/// \c no_serializer from it without pulling in the rest of the write side.
4///
5/// Copyright (c) 2026 by Travis Gockel. All rights reserved.
6///
7/// This program is free software: you can redistribute it and/or modify it under the terms of the Apache License
8/// as published by the Apache Software Foundation, either version 2 of the License, or (at your option) any later
9/// version.
10///
11/// \author Travis Gockel (travis@gockelhut.com)
12#pragma once
13
14#include <jsonv/config.hpp>
15#include <jsonv/forward.hpp>
16#include <jsonv/path.hpp>
17
18#include <exception>
19#include <stdexcept>
20#include <string>
21#include <string_view>
22#include <typeindex>
23#include <typeinfo>
24
25namespace jsonv
26{
27
28/// \addtogroup Serialization
29/// \{
30
31/// Exception thrown if there is any problem running \c serialize or \c to_json.
32///
33/// It says where the failure was found, as the \ref path of the slot the failing \c serializer was writing into, and
34/// what was being serialized there, as the demangled \ref type_name. Whatever the serializer threw is kept in
35/// \ref nested_ptr, unless the failure was thrown as a \c serialization_error to begin with, as a \c no_serializer is.
36///
37/// The path is the \c writer::current_path of the writer the failing serializer was handed, so it names a position in
38/// the document being written. A serializer on the \c value bridge -- a \c value_serializer_for, a
39/// \c value_adapter_for, a function returning a \c value, or a \c polymorphic_adapter subtype registered with
40/// \c keyed_subtype_action::check or \c keyed_subtype_action::insert -- serializes its parts through a writer of its
41/// own, so a failure inside one of those reports a path from that tree's root rather than from the document's.
43 public std::runtime_error
44{
45public:
46 /// Create a new exception for a failure at \a path while serializing \a type, described by \a message and, if an
47 /// exception caused it, carrying that exception as \a cause.
49 const std::type_info& type,
50 std::string message,
51 std::exception_ptr cause = nullptr
52 );
53
54 /// As above, naming the type by its \c std::type_index.
56 const std::type_index& type,
57 std::string message,
58 std::exception_ptr cause = nullptr
59 );
60
61 virtual ~serialization_error() noexcept;
62
63 /// The path of the slot the failing serializer was writing into. This is empty for a failure at the root of the
64 /// document, and for one which did not happen while writing, such as a \c no_serializer thrown by
65 /// \c formats::get_serializer.
67 const jsonv::path& path() const noexcept;
68
69 /// Get an ID for the type which was being serialized.
71 std::type_index type_index() const noexcept;
72
73 /// The demangled name of the type which was being serialized.
75 std::string_view type_name() const noexcept;
76
77 /// Human-readable details about what went wrong, without the position \c what prefixes them with.
79 const std::string& message() const noexcept;
80
81 /// The exception which caused this one, if there was one: what the \c serializer threw. This is \c nullptr when the
82 /// failure was thrown as a \c serialization_error in the first place.
84 const std::exception_ptr& nested_ptr() const noexcept;
85
86private:
87 explicit serialization_error(jsonv::path path,
88 const std::type_index& type,
89 std::string type_name,
90 std::string message,
91 std::exception_ptr cause
92 );
93
94private:
95 jsonv::path _path;
96 std::type_index _type_index;
97 std::string _type_name;
98 std::string _message;
99 std::exception_ptr _cause;
100};
101
102/// \}
103
104}
Represents an exact path in some JSON structure.
Definition path.hpp:107
Exception thrown if there is any problem running serialize or to_json.
serialization_error(jsonv::path path, const std::type_index &type, std::string message, std::exception_ptr cause=nullptr)
As above, naming the type by its std::type_index.
serialization_error(jsonv::path path, const std::type_info &type, std::string message, std::exception_ptr cause=nullptr)
Create a new exception for a failure at path while serializing type, described by message and,...
Copyright (c) 2014-2020 by Travis Gockel.
Copyright (c) 2012-2020 by Travis Gockel.
#define JSONV_NODISCARD
Warn if the caller discards the result of this function.
Definition config.hpp:132
#define JSONV_PUBLIC
This function or class is part of the public API for JSON Voorhees.
Definition config.hpp:113
STL namespace.
Support for JSONPath.