Skip to content

Commit

Permalink
code cleanup for #323
Browse files Browse the repository at this point in the history
  • Loading branch information
nlohmann committed Oct 10, 2016
1 parent 97280bb commit d3e7f9d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 68 deletions.
61 changes: 27 additions & 34 deletions src/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9457,10 +9457,27 @@ class basic_json
{
for (const auto& reference_token : reference_tokens)
{
// error condition (cf. RFC 6901, Sect. 4)
if (reference_token.size() > 1 and reference_token[0] == '0')
// convert null values to arrays or objects before continuing
if (ptr->m_type == value_t::null)
{
throw std::domain_error("array index must not begin with '0'");
// check if reference token is a number
const bool nums = std::all_of(reference_token.begin(),
reference_token.end(),
[](const char x)
{
return std::isdigit(x);
});

// change value to array for numbers or "-" or to object
// otherwise
if (nums or reference_token == "-")
{
*ptr = value_t::array;
}
else
{
*ptr = value_t::object;
}
}

switch (ptr->m_type)
Expand All @@ -9474,6 +9491,13 @@ class basic_json

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
Expand All @@ -9487,37 +9511,6 @@ class basic_json
break;
}

// null values are converted to arrays or objects
case value_t::null:
{
// check if reference token is a number
const bool nums = std::all_of(reference_token.begin(),
reference_token.end(),
[](const char x)
{
return std::isdigit(x);
});

if (nums)
{
// if reference token consists solely of numbers
// use it as array index -> create array
ptr = &ptr->operator[](static_cast<size_type>(std::stoi(reference_token)));
}
else if (reference_token == "-")
{
// explicityly treat "-" as index beyond the end
// which is 0 for an empty array -> create array
ptr = &ptr->operator[](0);
}
else
{
// treat reference token as key -> create object
ptr = &ptr->operator[](reference_token);
}
break;
}

default:
{
throw std::out_of_range("unresolved reference token '" + reference_token + "'");
Expand Down
61 changes: 27 additions & 34 deletions src/json.hpp.re2c
Original file line number Diff line number Diff line change
Expand Up @@ -8754,10 +8754,27 @@ class basic_json
{
for (const auto& reference_token : reference_tokens)
{
// error condition (cf. RFC 6901, Sect. 4)
if (reference_token.size() > 1 and reference_token[0] == '0')
// convert null values to arrays or objects before continuing
if (ptr->m_type == value_t::null)
{
throw std::domain_error("array index must not begin with '0'");
// check if reference token is a number
const bool nums = std::all_of(reference_token.begin(),
reference_token.end(),
[](const char x)
{
return std::isdigit(x);
});

// change value to array for numbers or "-" or to object
// otherwise
if (nums or reference_token == "-")
{
*ptr = value_t::array;
}
else
{
*ptr = value_t::object;
}
}

switch (ptr->m_type)
Expand All @@ -8771,6 +8788,13 @@ class basic_json

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
Expand All @@ -8784,37 +8808,6 @@ class basic_json
break;
}

// null values are converted to arrays or objects
case value_t::null:
{
// check if reference token is a number
const bool nums = std::all_of(reference_token.begin(),
reference_token.end(),
[](const char x)
{
return std::isdigit(x);
});

if (nums)
{
// if reference token consists solely of numbers
// use it as array index -> create array
ptr = &ptr->operator[](static_cast<size_type>(std::stoi(reference_token)));
}
else if (reference_token == "-")
{
// explicityly treat "-" as index beyond the end
// which is 0 for an empty array -> create array
ptr = &ptr->operator[](0);
}
else
{
// treat reference token as key -> create object
ptr = &ptr->operator[](reference_token);
}
break;
}

default:
{
throw std::out_of_range("unresolved reference token '" + reference_token + "'");
Expand Down

0 comments on commit d3e7f9d

Please sign in to comment.