Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 68 additions & 40 deletions include/nlohmann/detail/json_pointer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -876,58 +876,86 @@ class json_pointer
const BasicJsonType& value,
BasicJsonType& result)
{
switch (value.type())
struct flatten_task
{
case detail::value_t::array:
string_t reference_string;
const BasicJsonType* value;

flatten_task(string_t reference_string_, const BasicJsonType* value_) noexcept
: reference_string(std::move(reference_string_)), value(value_)
{}
};

std::vector<flatten_task> stack;
stack.emplace_back(reference_string, &value);

while (!stack.empty())
{
auto current = std::move(stack.back());
stack.pop_back();

switch (current.value->type())
{
if (value.m_data.m_value.array->empty())
{
// flatten empty array as null
result[reference_string] = nullptr;
}
else
case detail::value_t::array:
{
// iterate array and use index as a reference string
for (std::size_t i = 0; i < value.m_data.m_value.array->size(); ++i)
if (current.value->m_data.m_value.array->empty())
{
flatten(detail::concat<string_t>(reference_string, '/', std::to_string(i)),
value.m_data.m_value.array->operator[](i), result);
// flatten empty array as null
result[current.reference_string] = nullptr;
}
else
{
// iterate array and use index as a reference string
for (std::size_t i = current.value->m_data.m_value.array->size(); i > 0; --i)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment about why this is iterating backwards?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documented in 46d2b4d: children are pushed in reverse because the work stack is LIFO, preserving the original object iteration order. The issue #5393 regression passes in C++11 and C++20.

{
const auto index = i - 1;
stack.emplace_back(detail::concat<string_t>(current.reference_string, '/', std::to_string(index)),
&current.value->m_data.m_value.array->operator[](index));
}
}
break;
}
break;
}

case detail::value_t::object:
{
if (value.m_data.m_value.object->empty())
{
// flatten empty object as null
result[reference_string] = nullptr;
}
else
case detail::value_t::object:
{
// iterate object and use keys as reference string
for (const auto& element : *value.m_data.m_value.object)
if (current.value->m_data.m_value.object->empty())
{
flatten(detail::concat<string_t>(reference_string, '/', detail::escape(element.first)), element.second, result);
// flatten empty object as null
result[current.reference_string] = nullptr;
}
else
{
// iterate object and use keys as reference string
std::vector<flatten_task> children;
children.reserve(current.value->m_data.m_value.object->size());
for (const auto& element : *current.value->m_data.m_value.object)
{
children.emplace_back(detail::concat<string_t>(current.reference_string, '/', detail::escape(element.first)),
&element.second);
}
// Push children in reverse so the LIFO stack preserves object iteration order.
for (auto it = children.rbegin(); it != children.rend(); ++it)
{
stack.emplace_back(std::move(*it));
}
}
break;
}
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:
{
// add a primitive value with its reference string
result[reference_string] = value;
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:
{
// add a primitive value with its reference string
result[current.reference_string] = *current.value;
break;
}
}
}
}
Expand Down
108 changes: 68 additions & 40 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16660,58 +16660,86 @@ class json_pointer
const BasicJsonType& value,
BasicJsonType& result)
{
switch (value.type())
struct flatten_task
{
case detail::value_t::array:
string_t reference_string;
const BasicJsonType* value;

flatten_task(string_t reference_string_, const BasicJsonType* value_) noexcept
: reference_string(std::move(reference_string_)), value(value_)
{}
};

std::vector<flatten_task> stack;
stack.emplace_back(reference_string, &value);

while (!stack.empty())
{
auto current = std::move(stack.back());
stack.pop_back();

switch (current.value->type())
{
if (value.m_data.m_value.array->empty())
{
// flatten empty array as null
result[reference_string] = nullptr;
}
else
case detail::value_t::array:
{
// iterate array and use index as a reference string
for (std::size_t i = 0; i < value.m_data.m_value.array->size(); ++i)
if (current.value->m_data.m_value.array->empty())
{
// flatten empty array as null
result[current.reference_string] = nullptr;
}
else
{
flatten(detail::concat<string_t>(reference_string, '/', std::to_string(i)),
value.m_data.m_value.array->operator[](i), result);
// iterate array and use index as a reference string
for (std::size_t i = current.value->m_data.m_value.array->size(); i > 0; --i)
{
const auto index = i - 1;
stack.emplace_back(detail::concat<string_t>(current.reference_string, '/', std::to_string(index)),
&current.value->m_data.m_value.array->operator[](index));
}
}
break;
}
break;
}

case detail::value_t::object:
{
if (value.m_data.m_value.object->empty())
{
// flatten empty object as null
result[reference_string] = nullptr;
}
else
case detail::value_t::object:
{
// iterate object and use keys as reference string
for (const auto& element : *value.m_data.m_value.object)
if (current.value->m_data.m_value.object->empty())
{
// flatten empty object as null
result[current.reference_string] = nullptr;
}
else
{
flatten(detail::concat<string_t>(reference_string, '/', detail::escape(element.first)), element.second, result);
// iterate object and use keys as reference string
std::vector<flatten_task> children;
children.reserve(current.value->m_data.m_value.object->size());
for (const auto& element : *current.value->m_data.m_value.object)
{
children.emplace_back(detail::concat<string_t>(current.reference_string, '/', detail::escape(element.first)),
&element.second);
}
// Push children in reverse so the LIFO stack preserves object iteration order.
for (auto it = children.rbegin(); it != children.rend(); ++it)
{
stack.emplace_back(std::move(*it));
}
}
break;
}
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:
{
// add a primitive value with its reference string
result[reference_string] = value;
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:
{
// add a primitive value with its reference string
result[current.reference_string] = *current.value;
break;
}
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions tests/src/unit-regression2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1571,4 +1571,14 @@ TEST_CASE("issue #5402 - update(merge_objects=true) overwrites a primitive with
CHECK(mixed == json({{"keep", {{"a", 1}, {"b", 2}}}, {"replace", {{"x", 2}}}}));
}

TEST_CASE("issue #5393 - flatten deeply nested values without overflowing the stack")
{
constexpr std::size_t depth = 100000;
const auto deep = json::parse(std::string(depth, '[') + "0" + std::string(depth, ']'));
const auto flattened = deep.flatten();
CHECK(flattened.size() == 1);
CHECK(flattened.begin().value() == 0);
CHECK(flattened.begin().key().size() == depth * 2);
}

DOCTEST_CLANG_SUPPRESS_WARNING_POP
Loading