mirror of
https://github.com/nlohmann/json.git
synced 2026-10-05 22:20:30 +00:00
Remove duplicate const overload of json_pointer::get_checked
The const and non-const get_checked() overloads had byte-identical 50-line bodies, differing only in the signature. The remaining template deduces a const-qualified BasicJsonType for const callers, so at(), the out_of_range::create() calls and the bounds check all still work. Part of #5708 Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
@@ -583,63 +583,6 @@ class json_pointer
|
|||||||
return *ptr;
|
return *ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
|
||||||
@throw parse_error.106 if an array index begins with '0'
|
|
||||||
@throw parse_error.109 if an array index was not a number
|
|
||||||
@throw out_of_range.402 if the array index '-' is used
|
|
||||||
@throw out_of_range.404 if the JSON pointer can not be resolved
|
|
||||||
*/
|
|
||||||
template<typename BasicJsonType>
|
|
||||||
const BasicJsonType& get_checked(const BasicJsonType* ptr) const
|
|
||||||
{
|
|
||||||
for (const auto& reference_token : reference_tokens)
|
|
||||||
{
|
|
||||||
switch (ptr->type())
|
|
||||||
{
|
|
||||||
case detail::value_t::object:
|
|
||||||
{
|
|
||||||
// note: at performs range check
|
|
||||||
ptr = &ptr->at(reference_token);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
case detail::value_t::array:
|
|
||||||
{
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
|
|
||||||
{
|
|
||||||
// "-" always fails the range check
|
|
||||||
JSON_THROW(detail::out_of_range::create(402, detail::concat(
|
|
||||||
"array index '-' (", std::to_string(ptr->m_data.m_value.array->size()),
|
|
||||||
") is out of range"), ptr));
|
|
||||||
}
|
|
||||||
|
|
||||||
const auto idx = array_index<BasicJsonType>(reference_token);
|
|
||||||
// Bounds check before access to avoid exception with JSON_NOEXCEPTION
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(idx >= ptr->m_data.m_value.array->size()))
|
|
||||||
{
|
|
||||||
JSON_THROW(detail::out_of_range::create(401, detail::concat(
|
|
||||||
"array index ", std::to_string(idx), " is out of range"), ptr));
|
|
||||||
}
|
|
||||||
ptr = &ptr->operator[](idx);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
case detail::value_t::null:
|
|
||||||
case detail::value_t::string:
|
|
||||||
case detail::value_t::boolean:
|
|
||||||
case detail::value_t::number_integer:
|
|
||||||
case detail::value_t::number_unsigned:
|
|
||||||
case detail::value_t::number_float:
|
|
||||||
case detail::value_t::binary:
|
|
||||||
case detail::value_t::discarded:
|
|
||||||
default:
|
|
||||||
JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", reference_token, "'"), ptr));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return *ptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@brief return a pointer to the pointed to value, or `nullptr` if the
|
@brief return a pointer to the pointed to value, or `nullptr` if the
|
||||||
pointer cannot be resolved because a key is missing, an array
|
pointer cannot be resolved because a key is missing, an array
|
||||||
|
|||||||
@@ -19297,63 +19297,6 @@ class json_pointer
|
|||||||
return *ptr;
|
return *ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
|
||||||
@throw parse_error.106 if an array index begins with '0'
|
|
||||||
@throw parse_error.109 if an array index was not a number
|
|
||||||
@throw out_of_range.402 if the array index '-' is used
|
|
||||||
@throw out_of_range.404 if the JSON pointer can not be resolved
|
|
||||||
*/
|
|
||||||
template<typename BasicJsonType>
|
|
||||||
const BasicJsonType& get_checked(const BasicJsonType* ptr) const
|
|
||||||
{
|
|
||||||
for (const auto& reference_token : reference_tokens)
|
|
||||||
{
|
|
||||||
switch (ptr->type())
|
|
||||||
{
|
|
||||||
case detail::value_t::object:
|
|
||||||
{
|
|
||||||
// note: at performs range check
|
|
||||||
ptr = &ptr->at(reference_token);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
case detail::value_t::array:
|
|
||||||
{
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
|
|
||||||
{
|
|
||||||
// "-" always fails the range check
|
|
||||||
JSON_THROW(detail::out_of_range::create(402, detail::concat(
|
|
||||||
"array index '-' (", std::to_string(ptr->m_data.m_value.array->size()),
|
|
||||||
") is out of range"), ptr));
|
|
||||||
}
|
|
||||||
|
|
||||||
const auto idx = array_index<BasicJsonType>(reference_token);
|
|
||||||
// Bounds check before access to avoid exception with JSON_NOEXCEPTION
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(idx >= ptr->m_data.m_value.array->size()))
|
|
||||||
{
|
|
||||||
JSON_THROW(detail::out_of_range::create(401, detail::concat(
|
|
||||||
"array index ", std::to_string(idx), " is out of range"), ptr));
|
|
||||||
}
|
|
||||||
ptr = &ptr->operator[](idx);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
case detail::value_t::null:
|
|
||||||
case detail::value_t::string:
|
|
||||||
case detail::value_t::boolean:
|
|
||||||
case detail::value_t::number_integer:
|
|
||||||
case detail::value_t::number_unsigned:
|
|
||||||
case detail::value_t::number_float:
|
|
||||||
case detail::value_t::binary:
|
|
||||||
case detail::value_t::discarded:
|
|
||||||
default:
|
|
||||||
JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", reference_token, "'"), ptr));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return *ptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@brief return a pointer to the pointed to value, or `nullptr` if the
|
@brief return a pointer to the pointed to value, or `nullptr` if the
|
||||||
pointer cannot be resolved because a key is missing, an array
|
pointer cannot be resolved because a key is missing, an array
|
||||||
|
|||||||
Reference in New Issue
Block a user