Skip to content

Commit f81701d

Browse files
committed
Updating json::object and json::array erase to return an iterator.
1 parent 174113c commit f81701d

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

Release/include/cpprest/json.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -880,9 +880,10 @@ namespace json
880880
/// Deletes an element of the JSON array.
881881
/// </summary>
882882
/// <param name="position">A const_iterator to the element to delete.</param>
883-
void erase(const_iterator position)
883+
/// <returns>Iterator to the new location of the element following the erased element.</returns>
884+
iterator erase(const_iterator position)
884885
{
885-
m_elements.erase(position);
886+
return m_elements.erase(position);
886887
}
887888

888889
/// <summary>
@@ -1094,9 +1095,10 @@ namespace json
10941095
/// Deletes an element of the JSON object.
10951096
/// </summary>
10961097
/// <param name="position">A const_iterator to the element to delete.</param>
1097-
void erase(const_iterator position)
1098+
/// <returns>Iterator to the new location of the element following the erased element.</returns>
1099+
iterator erase(const_iterator position)
10981100
{
1099-
m_elements.erase(position);
1101+
return m_elements.erase(position);
11001102
}
11011103

11021104
/// <summary>

Release/tests/functional/json/to_as_and_operators_tests.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -358,13 +358,15 @@ TEST(erase_array_iter)
358358
a[2] = json::value(3);
359359

360360
auto iter = a.as_array().begin() + 1;
361-
a.as_array().erase(iter);
361+
auto afterLoc = a.as_array().erase(iter);
362+
VERIFY_ARE_EQUAL(3, afterLoc->as_integer());
362363
VERIFY_ARE_EQUAL(2, a.size());
363364
VERIFY_ARE_EQUAL(1, a[0].as_integer());
364365
VERIFY_ARE_EQUAL(3, a[1].as_integer());
365366

366367
auto citer = a.as_array().cbegin() + 1;
367-
a.as_array().erase(citer);
368+
afterLoc = a.as_array().erase(citer);
369+
VERIFY_ARE_EQUAL(a.as_array().end(), afterLoc);
368370
VERIFY_ARE_EQUAL(1, a.size());
369371
VERIFY_ARE_EQUAL(1, a[0].as_integer());
370372
}
@@ -398,15 +400,17 @@ TEST(erase_object_iter)
398400
o[U("d")] = json::value(4);
399401

400402
auto iter = o.as_object().begin() + 1;
401-
o.as_object().erase(iter);
403+
auto afterLoc = o.as_object().erase(iter);
402404
VERIFY_ARE_EQUAL(3, o.size());
405+
VERIFY_ARE_EQUAL(3, afterLoc->second.as_integer());
403406
VERIFY_ARE_EQUAL(1, o[U("a")].as_integer());
404407
VERIFY_ARE_EQUAL(3, o[U("c")].as_integer());
405408
VERIFY_ARE_EQUAL(4, o[U("d")].as_integer());
406409

407410
auto citer = o.as_object().begin() + 2;
408-
o.as_object().erase(citer);
411+
afterLoc = o.as_object().erase(citer);
409412
VERIFY_ARE_EQUAL(2, o.size());
413+
VERIFY_ARE_EQUAL(o.as_object().end(), afterLoc);
410414
VERIFY_ARE_EQUAL(1, o[U("a")].as_integer());
411415
VERIFY_ARE_EQUAL(3, o[U("c")].as_integer());
412416
}

0 commit comments

Comments
 (0)