8 #include <nlohmann/detail/value_t.hpp>
9 #include <nlohmann/detail/string_escape.hpp>
10 #include <nlohmann/detail/input/position_t.hpp>
11 #include <nlohmann/detail/macro_scope.hpp>
53 const char*
what() const noexcept
override
62 JSON_HEDLEY_NON_NULL(3)
63 exception(
int id_, const
char* what_arg) :
id(id_), m(what_arg) {}
65 static std::string name(
const std::string& ename,
int id_)
67 return "[json.exception." + ename +
"." + std::to_string(id_) +
"] ";
70 template<
typename BasicJsonType>
71 static std::string diagnostics(
const BasicJsonType& leaf_element)
74 std::vector<std::string> tokens;
75 for (
const auto* current = &leaf_element; current->m_parent !=
nullptr; current = current->m_parent)
77 switch (current->m_parent->type())
81 for (std::size_t i = 0; i < current->m_parent->m_value.array->size(); ++i)
83 if (¤t->m_parent->m_value.array->operator[](i) == current)
85 tokens.emplace_back(std::to_string(i));
94 for (
const auto& element : *current->m_parent->m_value.object)
96 if (&element.second == current)
98 tokens.emplace_back(element.first.c_str());
123 return "(" + std::accumulate(tokens.rbegin(), tokens.rend(), std::string{},
124 [](
const std::string & a,
const std::string & b)
126 return a +
"/" + detail::escape(b);
129 static_cast<void>(leaf_element);
136 std::runtime_error m;
196 template<
typename BasicJsonType>
199 std::string w = exception::name(
"parse_error", id_) +
"parse error" +
200 position_string(pos) +
": " + exception::diagnostics(context) + what_arg;
204 template<
typename BasicJsonType>
205 static parse_error create(
int id_, std::size_t byte_,
const std::string& what_arg,
const BasicJsonType& context)
207 std::string w = exception::name(
"parse_error", id_) +
"parse error" +
208 (byte_ != 0 ? (
" at byte " + std::to_string(byte_)) :
"") +
209 ": " + exception::diagnostics(context) + what_arg;
225 parse_error(
int id_, std::size_t byte_,
const char* what_arg)
228 static std::string position_string(
const position_t& pos)
230 return " at line " + std::to_string(pos.
lines_read + 1) +
275 template<
typename BasicJsonType>
276 static invalid_iterator create(
int id_,
const std::string& what_arg,
const BasicJsonType& context)
278 std::string w = exception::name(
"invalid_iterator", id_) + exception::diagnostics(context) + what_arg;
283 JSON_HEDLEY_NON_NULL(3)
330 template<
typename BasicJsonType>
331 static type_error create(
int id_,
const std::string& what_arg,
const BasicJsonType& context)
333 std::string w = exception::name(
"type_error", id_) + exception::diagnostics(context) + what_arg;
338 JSON_HEDLEY_NON_NULL(3)
378 template<
typename BasicJsonType>
379 static out_of_range create(
int id_,
const std::string& what_arg,
const BasicJsonType& context)
381 std::string w = exception::name(
"out_of_range", id_) + exception::diagnostics(context) + what_arg;
386 JSON_HEDLEY_NON_NULL(3)
417 template<
typename BasicJsonType>
418 static other_error create(
int id_,
const std::string& what_arg,
const BasicJsonType& context)
420 std::string w = exception::name(
"other_error", id_) + exception::diagnostics(context) + what_arg;
425 JSON_HEDLEY_NON_NULL(3)
general exception of the basic_json class
Definition: exceptions.hpp:50
const char * what() const noexcept override
returns the explanatory string
Definition: exceptions.hpp:53
const int id
the id of the exception
Definition: exceptions.hpp:59
exception indicating errors with iterators
Definition: exceptions.hpp:273
exception indicating other library errors
Definition: exceptions.hpp:415
exception indicating access out of the defined range
Definition: exceptions.hpp:376
exception indicating a parse error
Definition: exceptions.hpp:185
const std::size_t byte
byte index of the parse error
Definition: exceptions.hpp:222
static parse_error create(int id_, const position_t &pos, const std::string &what_arg, const BasicJsonType &context)
create a parse error exception
Definition: exceptions.hpp:197
exception indicating executing a member function with a wrong type
Definition: exceptions.hpp:328
@ number_integer
number value (signed integer)
@ discarded
discarded by the parser callback function
@ binary
binary array (ordered collection of bytes)
@ object
object (unordered set of name/value pairs)
@ number_float
number value (floating-point)
@ number_unsigned
number value (unsigned integer)
@ array
array (ordered collection of values)
namespace for Niels Lohmann
Definition: adl_serializer.hpp:12
struct to capture the start position of the current token
Definition: position_t.hpp:11
std::size_t lines_read
the number of lines read
Definition: position_t.hpp:17
std::size_t chars_read_current_line
the number of characters read in the current line
Definition: position_t.hpp:15
std::size_t chars_read_total
the total number of characters read
Definition: position_t.hpp:13