1
0
mirror of https://github.com/nlohmann/json.git synced 2025-07-28 12:02:00 +03:00

♻️ move diagnostic code in header

This commit is contained in:
Niels Lohmann
2021-01-09 19:21:18 +01:00
parent 43cd5c8a4d
commit e160749003
13 changed files with 748 additions and 548 deletions

View File

@ -51,6 +51,7 @@ SOFTWARE.
#include <nlohmann/detail/conversions/from_json.hpp>
#include <nlohmann/detail/conversions/to_json.hpp>
#include <nlohmann/detail/exceptions.hpp>
#include <nlohmann/detail/diagnostics_t.hpp>
#include <nlohmann/detail/hash.hpp>
#include <nlohmann/detail/input/binary_reader.hpp>
#include <nlohmann/detail/input/input_adapters.hpp>
@ -189,10 +190,15 @@ class basic_json
friend class ::nlohmann::detail::json_sax_dom_parser;
template<typename BasicJsonType>
friend class ::nlohmann::detail::json_sax_dom_callback_parser;
template<typename BasicJsonType>
friend class ::nlohmann::detail::diagnostics_t;
/// workaround type for MSVC
using basic_json_t = NLOHMANN_BASIC_JSON_TPL;
/// shortcut
using diagnostics_t = ::nlohmann::detail::diagnostics_t<basic_json>;
JSON_PRIVATE_UNLESS_TESTED:
// convenience aliases for types residing in namespace detail;
using lexer = ::nlohmann::detail::lexer_base<basic_json>;
@ -1060,7 +1066,7 @@ class basic_json
object = nullptr; // silence warning, see #821
if (JSON_HEDLEY_UNLIKELY(t == value_t::null))
{
JSON_THROW(other_error::create(500, "961c151d2e87f2686a955a9be24d316f1362bf21 3.9.1")); // LCOV_EXCL_LINE
JSON_THROW(other_error::create(500, "961c151d2e87f2686a955a9be24d316f1362bf21 3.9.1", diagnostics_t())); // LCOV_EXCL_LINE
}
break;
}
@ -1629,7 +1635,7 @@ class basic_json
// if object is wanted but impossible, throw an exception
if (JSON_HEDLEY_UNLIKELY(manual_type == value_t::object && !is_an_object))
{
JSON_THROW(type_error::create(301, "cannot create object from initializer list"));
JSON_THROW(type_error::create(301, "cannot create object from initializer list", diagnostics_t()));
}
}
@ -1950,7 +1956,7 @@ class basic_json
// make sure iterator fits the current value
if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
{
JSON_THROW(invalid_iterator::create(201, diagnostics() + "iterators are not compatible"));
JSON_THROW(invalid_iterator::create(201, "iterators are not compatible", diagnostics_t()));
}
// copy type from first iterator
@ -1968,7 +1974,7 @@ class basic_json
if (JSON_HEDLEY_UNLIKELY(!first.m_it.primitive_iterator.is_begin()
|| !last.m_it.primitive_iterator.is_end()))
{
JSON_THROW(invalid_iterator::create(204, "iterators out of range"));
JSON_THROW(invalid_iterator::create(204, "iterators out of range", diagnostics_t()));
}
break;
}
@ -2042,8 +2048,7 @@ class basic_json
}
default:
JSON_THROW(invalid_iterator::create(206, "cannot construct with iterators from " +
std::string(first.m_object->type_name())));
JSON_THROW(invalid_iterator::create(206, "cannot construct with iterators from " + std::string(first.m_object->type_name()), diagnostics_t()));
}
assert_invariant();
@ -2763,60 +2768,6 @@ class basic_json
/// @}
std::string diagnostics() const
{
#if JSON_DIAGNOSTICS
std::vector<std::string> tokens;
for (const basic_json* current = this; current->m_parent != nullptr; current = current->m_parent)
{
switch (current->m_parent->type())
{
case value_t::array:
{
for (std::size_t i = 0; i < current->m_parent->m_value.array->size(); ++i)
{
if (current->m_parent->m_value.array->operator[](i) == *current)
{
tokens.emplace_back(std::to_string(i));
continue;
}
}
break;
}
case value_t::object:
{
for (const auto& element : *current->m_parent->m_value.object)
{
if (element.second == *current)
{
tokens.emplace_back(element.first.c_str());
continue;
}
}
break;
}
default:
break;
}
}
if (tokens.empty())
{
return "";
}
return "(" + std::accumulate(tokens.rbegin(), tokens.rend(), std::string{},
[](const std::string & a, const std::string & b)
{
return a + "/" + b;
}) + ") ";
#else
return "";
#endif
}
private:
//////////////////
// value access //
@ -2830,7 +2781,7 @@ class basic_json
return m_value.boolean;
}
JSON_THROW(type_error::create(302, diagnostics() + "type must be boolean, but is " + std::string(type_name())));
JSON_THROW(type_error::create(302, "type must be boolean, but is " + std::string(type_name()), diagnostics_t(*this)));
}
/// get a pointer to the value (object)
@ -2951,7 +2902,7 @@ class basic_json
return *ptr;
}
JSON_THROW(type_error::create(303, obj.diagnostics() + "incompatible ReferenceType for get_ref, actual type is " + std::string(obj.type_name())));
JSON_THROW(type_error::create(303, "incompatible ReferenceType for get_ref, actual type is " + std::string(obj.type_name()), diagnostics_t(obj)));
}
public:
@ -3379,7 +3330,7 @@ class basic_json
{
if (!is_binary())
{
JSON_THROW(type_error::create(302, diagnostics() + "type must be binary, but is " + std::string(type_name())));
JSON_THROW(type_error::create(302, "type must be binary, but is " + std::string(type_name()), diagnostics_t(*this)));
}
return *get_ptr<binary_t*>();
@ -3390,7 +3341,7 @@ class basic_json
{
if (!is_binary())
{
JSON_THROW(type_error::create(302, diagnostics() + "type must be binary, but is " + std::string(type_name())));
JSON_THROW(type_error::create(302, "type must be binary, but is " + std::string(type_name()), diagnostics_t(*this)));
}
return *get_ptr<const binary_t*>();
@ -3451,12 +3402,12 @@ class basic_json
JSON_CATCH (std::out_of_range&)
{
// create better exception explanation
JSON_THROW(out_of_range::create(401, diagnostics() + "array index " + std::to_string(idx) + " is out of range"));
JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", diagnostics_t(*this)));
}
}
else
{
JSON_THROW(type_error::create(304, diagnostics() + "cannot use at() with " + std::string(type_name())));
JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), diagnostics_t(*this)));
}
}
@ -3498,12 +3449,12 @@ class basic_json
JSON_CATCH (std::out_of_range&)
{
// create better exception explanation
JSON_THROW(out_of_range::create(401, diagnostics() + "array index " + std::to_string(idx) + " is out of range"));
JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", diagnostics_t(*this)));
}
}
else
{
JSON_THROW(type_error::create(304, diagnostics() + "cannot use at() with " + std::string(type_name())));
JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), diagnostics_t(*this)));
}
}
@ -3555,12 +3506,12 @@ class basic_json
JSON_CATCH (std::out_of_range&)
{
// create better exception explanation
JSON_THROW(out_of_range::create(403, diagnostics() + "key '" + key + "' not found"));
JSON_THROW(out_of_range::create(403, "key '" + key + "' not found", diagnostics_t(*this)));
}
}
else
{
JSON_THROW(type_error::create(304, diagnostics() + "cannot use at() with " + std::string(type_name())));
JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), diagnostics_t(*this)));
}
}
@ -3606,12 +3557,12 @@ class basic_json
JSON_CATCH (std::out_of_range&)
{
// create better exception explanation
JSON_THROW(out_of_range::create(403, diagnostics() + "key '" + key + "' not found"));
JSON_THROW(out_of_range::create(403, "key '" + key + "' not found", diagnostics_t(*this)));
}
}
else
{
JSON_THROW(type_error::create(304, diagnostics() + "cannot use at() with " + std::string(type_name())));
JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), diagnostics_t(*this)));
}
}
@ -3675,7 +3626,7 @@ class basic_json
return m_value.array->operator[](idx);
}
JSON_THROW(type_error::create(305, diagnostics() + "cannot use operator[] with a numeric argument with " + std::string(type_name())));
JSON_THROW(type_error::create(305, "cannot use operator[] with a numeric argument with " + std::string(type_name()), diagnostics_t(*this)));
}
/*!
@ -3705,7 +3656,7 @@ class basic_json
return m_value.array->operator[](idx);
}
JSON_THROW(type_error::create(305, diagnostics() + "cannot use operator[] with a numeric argument with " + std::string(type_name())));
JSON_THROW(type_error::create(305, "cannot use operator[] with a numeric argument with " + std::string(type_name()), diagnostics_t(*this)));
}
/*!
@ -3757,7 +3708,7 @@ class basic_json
#endif
}
JSON_THROW(type_error::create(305, diagnostics() + "cannot use operator[] with a string argument with " + std::string(type_name())));
JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), diagnostics_t(*this)));
}
/*!
@ -3799,7 +3750,7 @@ class basic_json
return m_value.object->find(key)->second;
}
JSON_THROW(type_error::create(305, diagnostics() + "cannot use operator[] with a string argument with " + std::string(type_name())));
JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), diagnostics_t(*this)));
}
/*!
@ -3853,7 +3804,7 @@ class basic_json
#endif
}
JSON_THROW(type_error::create(305, diagnostics() + "cannot use operator[] with a string argument with " + std::string(type_name())));
JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), diagnostics_t(*this)));
}
/*!
@ -3897,7 +3848,7 @@ class basic_json
return m_value.object->find(key)->second;
}
JSON_THROW(type_error::create(305, diagnostics() + "cannot use operator[] with a string argument with " + std::string(type_name())));
JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), diagnostics_t(*this)));
}
/*!
@ -3969,7 +3920,7 @@ class basic_json
return default_value;
}
JSON_THROW(type_error::create(306, diagnostics() + "cannot use value() with " + std::string(type_name())));
JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name()), diagnostics_t(*this)));
}
/*!
@ -4042,7 +3993,7 @@ class basic_json
}
}
JSON_THROW(type_error::create(306, diagnostics() + "cannot use value() with " + std::string(type_name())));
JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name()), diagnostics_t(*this)));
}
/*!
@ -4196,7 +4147,7 @@ class basic_json
// make sure iterator fits the current value
if (JSON_HEDLEY_UNLIKELY(this != pos.m_object))
{
JSON_THROW(invalid_iterator::create(202, diagnostics() + "iterator does not fit current value"));
JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", diagnostics_t(*this)));
}
IteratorType result = end();
@ -4212,7 +4163,7 @@ class basic_json
{
if (JSON_HEDLEY_UNLIKELY(!pos.m_it.primitive_iterator.is_begin()))
{
JSON_THROW(invalid_iterator::create(205, "iterator out of range"));
JSON_THROW(invalid_iterator::create(205, "iterator out of range", diagnostics_t(*this)));
}
if (is_string())
@ -4248,7 +4199,7 @@ class basic_json
}
default:
JSON_THROW(type_error::create(307, diagnostics() + "cannot use erase() with " + std::string(type_name())));
JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), diagnostics_t(*this)));
}
return result;
@ -4309,7 +4260,7 @@ class basic_json
// make sure iterator fits the current value
if (JSON_HEDLEY_UNLIKELY(this != first.m_object || this != last.m_object))
{
JSON_THROW(invalid_iterator::create(203, diagnostics() + "iterators do not fit current value"));
JSON_THROW(invalid_iterator::create(203, "iterators do not fit current value", diagnostics_t(*this)));
}
IteratorType result = end();
@ -4326,7 +4277,7 @@ class basic_json
if (JSON_HEDLEY_LIKELY(!first.m_it.primitive_iterator.is_begin()
|| !last.m_it.primitive_iterator.is_end()))
{
JSON_THROW(invalid_iterator::create(204, diagnostics() + "iterators out of range"));
JSON_THROW(invalid_iterator::create(204, "iterators out of range", diagnostics_t(*this)));
}
if (is_string())
@ -4364,7 +4315,7 @@ class basic_json
}
default:
JSON_THROW(type_error::create(307, diagnostics() + "cannot use erase() with " + std::string(type_name())));
JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), diagnostics_t(*this)));
}
return result;
@ -4407,7 +4358,7 @@ class basic_json
return m_value.object->erase(key);
}
JSON_THROW(type_error::create(307, diagnostics() + "cannot use erase() with " + std::string(type_name())));
JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), diagnostics_t(*this)));
}
/*!
@ -4441,14 +4392,14 @@ class basic_json
{
if (JSON_HEDLEY_UNLIKELY(idx >= size()))
{
JSON_THROW(out_of_range::create(401, diagnostics() + "array index " + std::to_string(idx) + " is out of range"));
JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", diagnostics_t(*this)));
}
m_value.array->erase(m_value.array->begin() + static_cast<difference_type>(idx));
}
else
{
JSON_THROW(type_error::create(307, diagnostics() + "cannot use erase() with " + std::string(type_name())));
JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), diagnostics_t(*this)));
}
}
@ -5393,7 +5344,7 @@ class basic_json
// push_back only works for null objects or arrays
if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array())))
{
JSON_THROW(type_error::create(308, diagnostics() + "cannot use push_back() with " + std::string(type_name())));
JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name()), diagnostics_t(*this)));
}
// transform null object into an array
@ -5431,7 +5382,7 @@ class basic_json
// push_back only works for null objects or arrays
if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array())))
{
JSON_THROW(type_error::create(308, diagnostics() + "cannot use push_back() with " + std::string(type_name())));
JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name()), diagnostics_t(*this)));
}
// transform null object into an array
@ -5484,7 +5435,7 @@ class basic_json
// push_back only works for null objects or objects
if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_object())))
{
JSON_THROW(type_error::create(308, diagnostics() + "cannot use push_back() with " + std::string(type_name())));
JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name()), diagnostics_t(*this)));
}
// transform null object into an object
@ -5592,7 +5543,7 @@ class basic_json
// emplace_back only works for null objects or arrays
if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array())))
{
JSON_THROW(type_error::create(311, diagnostics() + "cannot use emplace_back() with " + std::string(type_name())));
JSON_THROW(type_error::create(311, "cannot use emplace_back() with " + std::string(type_name()), diagnostics_t(*this)));
}
// transform null object into an array
@ -5654,7 +5605,7 @@ class basic_json
// emplace only works for null objects or arrays
if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_object())))
{
JSON_THROW(type_error::create(311, diagnostics() + "cannot use emplace() with " + std::string(type_name())));
JSON_THROW(type_error::create(311, "cannot use emplace() with " + std::string(type_name()), diagnostics_t(*this)));
}
// transform null object into an object
@ -5730,7 +5681,7 @@ class basic_json
// check if iterator pos fits to this JSON value
if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
{
JSON_THROW(invalid_iterator::create(202, diagnostics() + "iterator does not fit current value"));
JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", diagnostics_t(*this)));
}
// insert to array and return iterator
@ -5743,7 +5694,7 @@ class basic_json
#endif
}
JSON_THROW(type_error::create(309, diagnostics() + "cannot use insert() with " + std::string(type_name())));
JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), diagnostics_t(*this)));
}
/*!
@ -5787,7 +5738,7 @@ class basic_json
// check if iterator pos fits to this JSON value
if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
{
JSON_THROW(invalid_iterator::create(202, diagnostics() + "iterator does not fit current value"));
JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", diagnostics_t(*this)));
}
// insert to array and return iterator
@ -5803,7 +5754,7 @@ class basic_json
#endif
}
JSON_THROW(type_error::create(309, diagnostics() + "cannot use insert() with " + std::string(type_name())));
JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), diagnostics_t(*this)));
}
/*!
@ -5841,24 +5792,24 @@ class basic_json
// insert only works for arrays
if (JSON_HEDLEY_UNLIKELY(!is_array()))
{
JSON_THROW(type_error::create(309, diagnostics() + "cannot use insert() with " + std::string(type_name())));
JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), diagnostics_t(*this)));
}
// check if iterator pos fits to this JSON value
if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
{
JSON_THROW(invalid_iterator::create(202, diagnostics() + "iterator does not fit current value"));
JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", diagnostics_t(*this)));
}
// check if range iterators belong to the same JSON object
if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
{
JSON_THROW(invalid_iterator::create(210, diagnostics() + "iterators do not fit"));
JSON_THROW(invalid_iterator::create(210, "iterators do not fit", diagnostics_t(*this)));
}
if (JSON_HEDLEY_UNLIKELY(first.m_object == this))
{
JSON_THROW(invalid_iterator::create(211, diagnostics() + "passed iterators may not belong to container"));
JSON_THROW(invalid_iterator::create(211, "passed iterators may not belong to container", diagnostics_t(*this)));
}
// insert to array and return iterator
@ -5903,13 +5854,13 @@ class basic_json
// insert only works for arrays
if (JSON_HEDLEY_UNLIKELY(!is_array()))
{
JSON_THROW(type_error::create(309, diagnostics() + "cannot use insert() with " + std::string(type_name())));
JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), diagnostics_t(*this)));
}
// check if iterator pos fits to this JSON value
if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
{
JSON_THROW(invalid_iterator::create(202, diagnostics() + "iterator does not fit current value"));
JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", diagnostics_t(*this)));
}
// insert to array and return iterator
@ -5954,19 +5905,19 @@ class basic_json
// insert only works for objects
if (JSON_HEDLEY_UNLIKELY(!is_object()))
{
JSON_THROW(type_error::create(309, diagnostics() + "cannot use insert() with " + std::string(type_name())));
JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), diagnostics_t(*this)));
}
// check if range iterators belong to the same JSON object
if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
{
JSON_THROW(invalid_iterator::create(210, diagnostics() + "iterators do not fit"));
JSON_THROW(invalid_iterator::create(210, "iterators do not fit", diagnostics_t(*this)));
}
// passed iterators must belong to objects
if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object()))
{
JSON_THROW(invalid_iterator::create(202, diagnostics() + "iterators first and last must point to objects"));
JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects", diagnostics_t(*this)));
}
m_value.object->insert(first.m_it.object_iterator, last.m_it.object_iterator);
@ -6003,11 +5954,11 @@ class basic_json
if (JSON_HEDLEY_UNLIKELY(!is_object()))
{
JSON_THROW(type_error::create(312, diagnostics() + "cannot use update() with " + std::string(type_name())));
JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(type_name()), diagnostics_t(*this)));
}
if (JSON_HEDLEY_UNLIKELY(!j.is_object()))
{
JSON_THROW(type_error::create(312, diagnostics() + "cannot use update() with " + std::string(j.type_name())));
JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(j.type_name()), diagnostics_t(*this)));
}
for (auto it = j.cbegin(); it != j.cend(); ++it)
@ -6054,20 +6005,20 @@ class basic_json
if (JSON_HEDLEY_UNLIKELY(!is_object()))
{
JSON_THROW(type_error::create(312, diagnostics() + "cannot use update() with " + std::string(type_name())));
JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(type_name()), diagnostics_t(*this)));
}
// check if range iterators belong to the same JSON object
if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
{
JSON_THROW(invalid_iterator::create(210, diagnostics() + "iterators do not fit"));
JSON_THROW(invalid_iterator::create(210, "iterators do not fit", diagnostics_t(*this)));
}
// passed iterators must belong to objects
if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object()
|| !last.m_object->is_object()))
{
JSON_THROW(invalid_iterator::create(202, diagnostics() + "iterators first and last must point to objects"));
JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects", diagnostics_t(*this)));
}
for (auto it = first; it != last; ++it)
@ -6162,7 +6113,7 @@ class basic_json
}
else
{
JSON_THROW(type_error::create(310, diagnostics() + "cannot use swap() with " + std::string(type_name())));
JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), diagnostics_t(*this)));
}
}
@ -6195,7 +6146,7 @@ class basic_json
}
else
{
JSON_THROW(type_error::create(310, diagnostics() + "cannot use swap() with " + std::string(type_name())));
JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), diagnostics_t(*this)));
}
}
@ -6228,7 +6179,7 @@ class basic_json
}
else
{
JSON_THROW(type_error::create(310, diagnostics() + "cannot use swap() with " + std::string(type_name())));
JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), diagnostics_t(*this)));
}
}
@ -6261,7 +6212,7 @@ class basic_json
}
else
{
JSON_THROW(type_error::create(310, diagnostics() + "cannot use swap() with " + std::string(type_name())));
JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), diagnostics_t(*this)));
}
}
@ -6275,7 +6226,7 @@ class basic_json
}
else
{
JSON_THROW(type_error::create(310, diagnostics() + "cannot use swap() with " + std::string(type_name())));
JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), diagnostics_t(*this)));
}
}
@ -8484,7 +8435,7 @@ class basic_json
if (JSON_HEDLEY_UNLIKELY(idx > parent.size()))
{
// avoid undefined behavior
JSON_THROW(out_of_range::create(401, parent.diagnostics() + "array index " + std::to_string(idx) + " is out of range"));
JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", diagnostics_t(parent)));
}
// default case: insert add offset
@ -8518,7 +8469,7 @@ class basic_json
}
else
{
JSON_THROW(out_of_range::create(403, diagnostics() + "key '" + last_path + "' not found"));
JSON_THROW(out_of_range::create(403, "key '" + last_path + "' not found", diagnostics_t(*this)));
}
}
else if (parent.is_array())
@ -8531,7 +8482,7 @@ class basic_json
// type check: top level value must be an array
if (JSON_HEDLEY_UNLIKELY(!json_patch.is_array()))
{
JSON_THROW(parse_error::create(104, 0, diagnostics() + "JSON patch must be an array of objects"));
JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects", diagnostics_t(json_patch)));
}
// iterate and apply the operations
@ -8551,13 +8502,13 @@ class basic_json
// check if desired value is present
if (JSON_HEDLEY_UNLIKELY(it == val.m_value.object->end()))
{
JSON_THROW(parse_error::create(105, 0, diagnostics() + error_msg + " must have member '" + member + "'"));
JSON_THROW(parse_error::create(105, 0, error_msg + " must have member '" + member + "'", diagnostics_t(val)));
}
// check if result is of type string
if (JSON_HEDLEY_UNLIKELY(string_type && !it->second.is_string()))
{
JSON_THROW(parse_error::create(105, 0, diagnostics() + error_msg + " must have string member '" + member + "'"));
JSON_THROW(parse_error::create(105, 0, error_msg + " must have string member '" + member + "'", diagnostics_t(val)));
}
// no error: return value
@ -8567,7 +8518,7 @@ class basic_json
// type check: every element of the array must be an object
if (JSON_HEDLEY_UNLIKELY(!val.is_object()))
{
JSON_THROW(parse_error::create(104, 0, diagnostics() + "JSON patch must be an array of objects"));
JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects", diagnostics_t(val)));
}
// collect mandatory members
@ -8645,7 +8596,7 @@ class basic_json
// throw an exception if test fails
if (JSON_HEDLEY_UNLIKELY(!success))
{
JSON_THROW(other_error::create(501, val.diagnostics() + "unsuccessful: " + val.dump()));
JSON_THROW(other_error::create(501, "unsuccessful: " + val.dump(), diagnostics_t(val)));
}
break;
@ -8655,7 +8606,7 @@ class basic_json
{
// op must be "add", "remove", "replace", "move", "copy", or
// "test"
JSON_THROW(parse_error::create(105, 0, diagnostics() + "operation value '" + op + "' is invalid"));
JSON_THROW(parse_error::create(105, 0, "operation value '" + op + "' is invalid", diagnostics_t(val)));
}
}
}