Skip to content

Commit c970175

Browse files
committed
unflatten
1 parent f5c31b9 commit c970175

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

include/jsoncons_ext/jsonpointer/jsonpointer.hpp

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1552,7 +1552,9 @@ namespace jsonpointer {
15521552
Json unflatten_object(Iterator first, Iterator last, std::size_t offset, unflatten_options options)
15531553
{
15541554
Json jo{json_object_arg};
1555-
for (auto it = first; it != last; ++it)
1555+
1556+
auto it = first;
1557+
while (it != last)
15561558
{
15571559
if (it->first.tokens().size() <= offset)
15581560
{
@@ -1562,16 +1564,17 @@ namespace jsonpointer {
15621564
if (offset + 1 == it->first.tokens().size())
15631565
{
15641566
jo.try_emplace(*jt, *(it->second));
1567+
++it;
15651568
}
1566-
else if (!jo.contains(*jt))
1569+
else
15671570
{
1571+
auto last2 = find_last(it, last, offset, *jt);
15681572
if (options == unflatten_options{})
15691573
{
1570-
auto res = try_unflatten_array<Json,Iterator>(it, last, offset+1);
1574+
auto res = try_unflatten_array<Json,Iterator>(it, last2, offset+1);
15711575
if (!res)
15721576
{
1573-
jo.try_emplace(*jt, unflatten_object<Json,Iterator>(it,
1574-
find_last(it, last, offset, *jt), offset+1, options));
1577+
jo.try_emplace(*jt, unflatten_object<Json,Iterator>(it, last2, offset+1, options));
15751578
}
15761579
else
15771580
{
@@ -1580,9 +1583,9 @@ namespace jsonpointer {
15801583
}
15811584
else
15821585
{
1583-
jo.try_emplace(*jt, unflatten_object<Json,Iterator>(it,
1584-
find_last(it, last, offset, *jt), offset+1, options));
1586+
jo.try_emplace(*jt, unflatten_object<Json,Iterator>(it, last2, offset+1, options));
15851587
}
1588+
it = last2;
15861589
}
15871590
}
15881591
return jsoncons::optional<Json>{std::move(jo)};
@@ -1592,7 +1595,9 @@ namespace jsonpointer {
15921595
jsoncons::optional<Json> try_unflatten_array(Iterator first, Iterator last, std::size_t offset)
15931596
{
15941597
std::map<std::size_t,Json> m;
1595-
for (auto it = first; it != last; ++it)
1598+
1599+
auto it = first;
1600+
while (it != last)
15961601
{
15971602
if (offset >= it->first.tokens().size())
15981603
{
@@ -1609,20 +1614,21 @@ namespace jsonpointer {
16091614
if (offset + 1 == it->first.tokens().size())
16101615
{
16111616
m.emplace(std::make_pair(n,*(it->second)));
1617+
++it;
16121618
}
1613-
else if (m.find(n) == m.end())
1619+
else
16141620
{
1615-
auto res = try_unflatten_array<Json,Iterator>(it, find_last(it, last, offset, *jt),
1616-
offset+1);
1621+
auto last2 = find_last(it, last, offset, *jt);
1622+
auto res = try_unflatten_array<Json,Iterator>(it, last2, offset+1);
16171623
if (!res)
16181624
{
1619-
m.emplace(std::make_pair(n,unflatten_object<Json,Iterator>(it, find_last(it, last, offset, *jt),
1620-
offset+1, unflatten_options{})));
1625+
m.emplace(std::make_pair(n,unflatten_object<Json,Iterator>(it, last2, offset+1, unflatten_options{})));
16211626
}
16221627
else
16231628
{
16241629
m.emplace(std::make_pair(n,std::move(*res)));
16251630
}
1631+
it = last2;
16261632
}
16271633
}
16281634

0 commit comments

Comments
 (0)