Skip to content

Commit 46d2b4d

Browse files
committed
Address iterative flatten review feedback
Signed-off-by: dajiaohuang <mikewushuwen@outlook.com>
1 parent 841ee12 commit 46d2b4d

2 files changed

Lines changed: 22 additions & 12 deletions

File tree

include/nlohmann/detail/json_pointer.hpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -880,10 +880,14 @@ class json_pointer
880880
{
881881
string_t reference_string;
882882
const BasicJsonType* value;
883+
884+
flatten_task(string_t reference_string_, const BasicJsonType* value_)
885+
: reference_string(std::move(reference_string_)), value(value_)
886+
{}
883887
};
884888

885889
std::vector<flatten_task> stack;
886-
stack.push_back({reference_string, &value});
890+
stack.emplace_back(reference_string, &value);
887891

888892
while (!stack.empty())
889893
{
@@ -905,8 +909,8 @@ class json_pointer
905909
for (std::size_t i = current.value->m_data.m_value.array->size(); i > 0; --i)
906910
{
907911
const auto index = i - 1;
908-
stack.push_back({detail::concat<string_t>(current.reference_string, '/', std::to_string(index)),
909-
&current.value->m_data.m_value.array->operator[](index)});
912+
stack.emplace_back(detail::concat<string_t>(current.reference_string, '/', std::to_string(index)),
913+
&current.value->m_data.m_value.array->operator[](index));
910914
}
911915
}
912916
break;
@@ -926,12 +930,13 @@ class json_pointer
926930
children.reserve(current.value->m_data.m_value.object->size());
927931
for (const auto& element : *current.value->m_data.m_value.object)
928932
{
929-
children.push_back({detail::concat<string_t>(current.reference_string, '/', detail::escape(element.first)),
930-
&element.second});
933+
children.emplace_back(detail::concat<string_t>(current.reference_string, '/', detail::escape(element.first)),
934+
&element.second);
931935
}
936+
// Push children in reverse so the LIFO stack preserves object iteration order.
932937
for (auto it = children.rbegin(); it != children.rend(); ++it)
933938
{
934-
stack.push_back(std::move(*it));
939+
stack.emplace_back(std::move(*it));
935940
}
936941
}
937942
break;

single_include/nlohmann/json.hpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16664,10 +16664,14 @@ class json_pointer
1666416664
{
1666516665
string_t reference_string;
1666616666
const BasicJsonType* value;
16667+
16668+
flatten_task(string_t reference_string_, const BasicJsonType* value_)
16669+
: reference_string(std::move(reference_string_)), value(value_)
16670+
{}
1666716671
};
1666816672

1666916673
std::vector<flatten_task> stack;
16670-
stack.push_back({reference_string, &value});
16674+
stack.emplace_back(reference_string, &value);
1667116675

1667216676
while (!stack.empty())
1667316677
{
@@ -16689,8 +16693,8 @@ class json_pointer
1668916693
for (std::size_t i = current.value->m_data.m_value.array->size(); i > 0; --i)
1669016694
{
1669116695
const auto index = i - 1;
16692-
stack.push_back({detail::concat<string_t>(current.reference_string, '/', std::to_string(index)),
16693-
&current.value->m_data.m_value.array->operator[](index)});
16696+
stack.emplace_back(detail::concat<string_t>(current.reference_string, '/', std::to_string(index)),
16697+
&current.value->m_data.m_value.array->operator[](index));
1669416698
}
1669516699
}
1669616700
break;
@@ -16710,12 +16714,13 @@ class json_pointer
1671016714
children.reserve(current.value->m_data.m_value.object->size());
1671116715
for (const auto& element : *current.value->m_data.m_value.object)
1671216716
{
16713-
children.push_back({detail::concat<string_t>(current.reference_string, '/', detail::escape(element.first)),
16714-
&element.second});
16717+
children.emplace_back(detail::concat<string_t>(current.reference_string, '/', detail::escape(element.first)),
16718+
&element.second);
1671516719
}
16720+
// Push children in reverse so the LIFO stack preserves object iteration order.
1671616721
for (auto it = children.rbegin(); it != children.rend(); ++it)
1671716722
{
16718-
stack.push_back(std::move(*it));
16723+
stack.emplace_back(std::move(*it));
1671916724
}
1672016725
}
1672116726
break;

0 commit comments

Comments
 (0)