mirror of
https://github.com/nlohmann/json.git
synced 2025-07-31 10:24:23 +03:00
♻️ simplify destroy() function for primitive types
This commit is contained in:
@ -1133,51 +1133,53 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
|
|
||||||
void destroy(value_t t) noexcept
|
void destroy(value_t t) noexcept
|
||||||
{
|
{
|
||||||
// flatten the current json_value to a heap-allocated stack
|
if (t == value_t::array || t == value_t::object)
|
||||||
std::vector<basic_json> stack;
|
{
|
||||||
|
// flatten the current json_value to a heap-allocated stack
|
||||||
|
std::vector<basic_json> stack;
|
||||||
|
|
||||||
// move the top-level items to stack
|
// move the top-level items to stack
|
||||||
if (t == value_t::array)
|
if (t == value_t::array)
|
||||||
{
|
|
||||||
stack.reserve(array->size());
|
|
||||||
std::move(array->begin(), array->end(), std::back_inserter(stack));
|
|
||||||
}
|
|
||||||
else if (t == value_t::object)
|
|
||||||
{
|
|
||||||
stack.reserve(object->size());
|
|
||||||
for (auto&& it : *object)
|
|
||||||
{
|
{
|
||||||
stack.push_back(std::move(it.second));
|
stack.reserve(array->size());
|
||||||
|
std::move(array->begin(), array->end(), std::back_inserter(stack));
|
||||||
}
|
}
|
||||||
}
|
else
|
||||||
|
|
||||||
while (!stack.empty())
|
|
||||||
{
|
|
||||||
// move the last item to local variable to be processed
|
|
||||||
basic_json current_item(std::move(stack.back()));
|
|
||||||
stack.pop_back();
|
|
||||||
|
|
||||||
// if current_item is array/object, move
|
|
||||||
// its children to the stack to be processed later
|
|
||||||
if (current_item.is_array())
|
|
||||||
{
|
{
|
||||||
std::move(current_item.m_value.array->begin(), current_item.m_value.array->end(),
|
stack.reserve(object->size());
|
||||||
std::back_inserter(stack));
|
for (auto&& it : *object)
|
||||||
|
|
||||||
current_item.m_value.array->clear();
|
|
||||||
}
|
|
||||||
else if (current_item.is_object())
|
|
||||||
{
|
|
||||||
for (auto&& it : *current_item.m_value.object)
|
|
||||||
{
|
{
|
||||||
stack.push_back(std::move(it.second));
|
stack.push_back(std::move(it.second));
|
||||||
}
|
}
|
||||||
|
|
||||||
current_item.m_value.object->clear();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// it's now safe that current_item get destructed
|
while (!stack.empty())
|
||||||
// since it doesn't have any children
|
{
|
||||||
|
// move the last item to local variable to be processed
|
||||||
|
basic_json current_item(std::move(stack.back()));
|
||||||
|
stack.pop_back();
|
||||||
|
|
||||||
|
// if current_item is array/object, move
|
||||||
|
// its children to the stack to be processed later
|
||||||
|
if (current_item.is_array())
|
||||||
|
{
|
||||||
|
std::move(current_item.m_value.array->begin(), current_item.m_value.array->end(), std::back_inserter(stack));
|
||||||
|
|
||||||
|
current_item.m_value.array->clear();
|
||||||
|
}
|
||||||
|
else if (current_item.is_object())
|
||||||
|
{
|
||||||
|
for (auto&& it : *current_item.m_value.object)
|
||||||
|
{
|
||||||
|
stack.push_back(std::move(it.second));
|
||||||
|
}
|
||||||
|
|
||||||
|
current_item.m_value.object->clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
// it's now safe that current_item get destructed
|
||||||
|
// since it doesn't have any children
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (t)
|
switch (t)
|
||||||
|
@ -18168,51 +18168,53 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
|
|
||||||
void destroy(value_t t) noexcept
|
void destroy(value_t t) noexcept
|
||||||
{
|
{
|
||||||
// flatten the current json_value to a heap-allocated stack
|
if (t == value_t::array || t == value_t::object)
|
||||||
std::vector<basic_json> stack;
|
{
|
||||||
|
// flatten the current json_value to a heap-allocated stack
|
||||||
|
std::vector<basic_json> stack;
|
||||||
|
|
||||||
// move the top-level items to stack
|
// move the top-level items to stack
|
||||||
if (t == value_t::array)
|
if (t == value_t::array)
|
||||||
{
|
|
||||||
stack.reserve(array->size());
|
|
||||||
std::move(array->begin(), array->end(), std::back_inserter(stack));
|
|
||||||
}
|
|
||||||
else if (t == value_t::object)
|
|
||||||
{
|
|
||||||
stack.reserve(object->size());
|
|
||||||
for (auto&& it : *object)
|
|
||||||
{
|
{
|
||||||
stack.push_back(std::move(it.second));
|
stack.reserve(array->size());
|
||||||
|
std::move(array->begin(), array->end(), std::back_inserter(stack));
|
||||||
}
|
}
|
||||||
}
|
else
|
||||||
|
|
||||||
while (!stack.empty())
|
|
||||||
{
|
|
||||||
// move the last item to local variable to be processed
|
|
||||||
basic_json current_item(std::move(stack.back()));
|
|
||||||
stack.pop_back();
|
|
||||||
|
|
||||||
// if current_item is array/object, move
|
|
||||||
// its children to the stack to be processed later
|
|
||||||
if (current_item.is_array())
|
|
||||||
{
|
{
|
||||||
std::move(current_item.m_value.array->begin(), current_item.m_value.array->end(),
|
stack.reserve(object->size());
|
||||||
std::back_inserter(stack));
|
for (auto&& it : *object)
|
||||||
|
|
||||||
current_item.m_value.array->clear();
|
|
||||||
}
|
|
||||||
else if (current_item.is_object())
|
|
||||||
{
|
|
||||||
for (auto&& it : *current_item.m_value.object)
|
|
||||||
{
|
{
|
||||||
stack.push_back(std::move(it.second));
|
stack.push_back(std::move(it.second));
|
||||||
}
|
}
|
||||||
|
|
||||||
current_item.m_value.object->clear();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// it's now safe that current_item get destructed
|
while (!stack.empty())
|
||||||
// since it doesn't have any children
|
{
|
||||||
|
// move the last item to local variable to be processed
|
||||||
|
basic_json current_item(std::move(stack.back()));
|
||||||
|
stack.pop_back();
|
||||||
|
|
||||||
|
// if current_item is array/object, move
|
||||||
|
// its children to the stack to be processed later
|
||||||
|
if (current_item.is_array())
|
||||||
|
{
|
||||||
|
std::move(current_item.m_value.array->begin(), current_item.m_value.array->end(), std::back_inserter(stack));
|
||||||
|
|
||||||
|
current_item.m_value.array->clear();
|
||||||
|
}
|
||||||
|
else if (current_item.is_object())
|
||||||
|
{
|
||||||
|
for (auto&& it : *current_item.m_value.object)
|
||||||
|
{
|
||||||
|
stack.push_back(std::move(it.second));
|
||||||
|
}
|
||||||
|
|
||||||
|
current_item.m_value.object->clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
// it's now safe that current_item get destructed
|
||||||
|
// since it doesn't have any children
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (t)
|
switch (t)
|
||||||
|
Reference in New Issue
Block a user