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>
49class exception :
public std::exception
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;
184class parse_error :
public exception
196 template<
typename BasicJsonType>
197 static parse_error
create(
int id_,
const position_t& pos,
const std::string& what_arg,
const BasicJsonType& context)
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)
226 : exception(id_, what_arg),
byte(byte_) {}
228 static std::string position_string(
const position_t& pos)
230 return " at line " + std::to_string(pos.
lines_read + 1) +
272class invalid_iterator :
public exception
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;
279 return invalid_iterator(id_, w.c_str());
283 JSON_HEDLEY_NON_NULL(3)
284 invalid_iterator(
int id_,
const char* what_arg)
285 : exception(id_, what_arg) {}
327class type_error :
public exception
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;
334 return type_error(id_, w.c_str());
338 JSON_HEDLEY_NON_NULL(3)
339 type_error(
int id_,
const char* what_arg) : exception(id_, what_arg) {}
375class out_of_range :
public exception
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;
382 return out_of_range(id_, w.c_str());
386 JSON_HEDLEY_NON_NULL(3)
387 out_of_range(
int id_,
const char* what_arg) : exception(id_, what_arg) {}
414class other_error :
public exception
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;
421 return other_error(id_, w.c_str());
425 JSON_HEDLEY_NON_NULL(3)
426 other_error(
int id_,
const char* what_arg) : exception(id_, what_arg) {}
const int id
the id of the exception
Definition exceptions.hpp:59
const char * what() const noexcept override
returns the explanatory string
Definition exceptions.hpp:53
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
detail namespace with internal helper functions
Definition from_json.hpp:25
@ null
null value
Definition value_t.hpp:42
@ number_integer
number value (signed integer)
Definition value_t.hpp:47
@ boolean
boolean value
Definition value_t.hpp:46
@ discarded
discarded by the parser callback function
Definition value_t.hpp:51
@ binary
binary array (ordered collection of bytes)
Definition value_t.hpp:50
@ object
object (unordered set of name/value pairs)
Definition value_t.hpp:43
@ string
string value
Definition value_t.hpp:45
@ number_float
number value (floating-point)
Definition value_t.hpp:49
@ number_unsigned
number value (unsigned integer)
Definition value_t.hpp:48
@ array
array (ordered collection of values)
Definition value_t.hpp:44
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