1
0
mirror of https://github.com/nlohmann/json.git synced 2025-07-13 20:21:48 +03:00

improved RFC compliance and code coverage

This commit is contained in:
Niels
2016-04-17 18:54:54 +02:00
parent f883a04c87
commit 0835eb293f
3 changed files with 112 additions and 8 deletions

View File

@ -8302,7 +8302,9 @@ class basic_json
@complexity Linear in the length of the JSON pointer.
@throw std::out_of_range if the JSON pointer can not be resolved
@throw std::out_of_range if the JSON pointer can not be resolved
@throw std::domain_error if an array index begins with '0'
@throw std::invalid_argument if an array index was not a number
*/
reference get_unchecked(pointer ptr) const
{
@ -8312,18 +8314,27 @@ class basic_json
{
case value_t::object:
{
// use unchecked object access
ptr = &ptr->operator[](reference_token);
break;
}
case value_t::array:
{
// error condition (cf. RFC 6901, Sect. 4)
if (reference_token.size() > 1 and reference_token[0] == '0')
{
throw std::domain_error("array index must not begin with '0'");
}
if (reference_token == "-")
{
// explicityly treat "-" as index beyond the end
ptr = &ptr->operator[](ptr->m_value.array->size());
}
else
{
// convert array index to number; unchecked access
ptr = &ptr->operator[](static_cast<size_t>(std::stoi(reference_token)));
}
break;
@ -8347,6 +8358,7 @@ class basic_json
{
case value_t::object:
{
// note: at performs range check
ptr = &ptr->at(reference_token);
break;
}
@ -8355,12 +8367,20 @@ class basic_json
{
if (reference_token == "-")
{
throw std::out_of_range("cannot resolve reference token '-'");
// "-" always fails the range check
throw std::out_of_range("array index '-' (" +
std::to_string(ptr->m_value.array->size()) +
") is out of range");
}
else
// error condition (cf. RFC 6901, Sect. 4)
if (reference_token.size() > 1 and reference_token[0] == '0')
{
ptr = &ptr->at(static_cast<size_t>(std::stoi(reference_token)));
throw std::domain_error("array index must not begin with '0'");
}
// note: at performs range check
ptr = &ptr->at(static_cast<size_t>(std::stoi(reference_token)));
break;
}
@ -8390,6 +8410,7 @@ class basic_json
{
case value_t::object:
{
// use unchecked object access
ptr = &ptr->operator[](reference_token);
break;
}
@ -8398,10 +8419,19 @@ class basic_json
{
if (reference_token == "-")
{
// "-" cannot be used for const access
throw std::out_of_range("array index '-' (" +
std::to_string(ptr->m_value.array->size()) +
") is out of range");
}
// error condition (cf. RFC 6901, Sect. 4)
if (reference_token.size() > 1 and reference_token[0] == '0')
{
throw std::domain_error("array index must not begin with '0'");
}
// use unchecked array access
ptr = &ptr->operator[](static_cast<size_t>(std::stoi(reference_token)));
break;
}
@ -8424,6 +8454,7 @@ class basic_json
{
case value_t::object:
{
// note: at performs range check
ptr = &ptr->at(reference_token);
break;
}
@ -8432,10 +8463,19 @@ class basic_json
{
if (reference_token == "-")
{
// "-" always fails the range check
throw std::out_of_range("array index '-' (" +
std::to_string(ptr->m_value.array->size()) +
") is out of range");
}
// error condition (cf. RFC 6901, Sect. 4)
if (reference_token.size() > 1 and reference_token[0] == '0')
{
throw std::domain_error("array index must not begin with '0'");
}
// note: at performs range check
ptr = &ptr->at(static_cast<size_t>(std::stoi(reference_token)));
break;
}