Skip to content

Commit 174113c

Browse files
committed
Added the ability to erase elements from a JSON array or object.
1 parent 60f278d commit 174113c

File tree

3 files changed

+148
-0
lines changed

3 files changed

+148
-0
lines changed

Release/include/cpprest/json.h

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,18 @@ namespace json
564564
CASABLANCA_DEPRECATED("This API is deprecated and will be removed in a future release, use json::value::at() instead.")
565565
value get(const utility::string_t &key) const;
566566

567+
/// <summary>
568+
/// Erases an element of a JSON array. Throws if index is out of bounds.
569+
/// </summary>
570+
/// <param name="index">The index of the element to erase in the JSON array.</param>
571+
_ASYNCRTIMP void erase(size_t index);
572+
573+
/// <summary>
574+
/// Erases an element of a JSON object. Throws if the key doesn't exist.
575+
/// </summary>
576+
/// <param name="key">The key of the element to erase in the JSON object.</param>
577+
_ASYNCRTIMP void erase(const utility::string_t &key);
578+
567579
/// <summary>
568580
/// Accesses an element of a JSON array. Throws when index out of bounds.
569581
/// </summary>
@@ -864,6 +876,28 @@ namespace json
864876
return m_elements.crend();
865877
}
866878

879+
/// <summary>
880+
/// Deletes an element of the JSON array.
881+
/// </summary>
882+
/// <param name="position">A const_iterator to the element to delete.</param>
883+
void erase(const_iterator position)
884+
{
885+
m_elements.erase(position);
886+
}
887+
888+
/// <summary>
889+
/// Deletes the element at an index of the JSON array.
890+
/// </summary>
891+
/// <param name="index">The index of the element to delete.</param>
892+
void erase(size_type index)
893+
{
894+
if (index >= m_elements.size())
895+
{
896+
throw json_exception(_XPLATSTR("index out of bounds"));
897+
}
898+
m_elements.erase(m_elements.begin() + index);
899+
}
900+
867901
/// <summary>
868902
/// Accesses an element of a JSON array. Throws when index out of bounds.
869903
/// </summary>
@@ -1056,6 +1090,30 @@ namespace json
10561090
return m_elements.crend();
10571091
}
10581092

1093+
/// <summary>
1094+
/// Deletes an element of the JSON object.
1095+
/// </summary>
1096+
/// <param name="position">A const_iterator to the element to delete.</param>
1097+
void erase(const_iterator position)
1098+
{
1099+
m_elements.erase(position);
1100+
}
1101+
1102+
/// <summary>
1103+
/// Deletes an element of the JSON object. If the key doesn't exist, this method throws.
1104+
/// </summary>
1105+
/// <param name="key">The key of an element in the JSON object.</param>
1106+
void erase(const utility::string_t &key)
1107+
{
1108+
auto iter = find_by_key(key);
1109+
if (iter == m_elements.end() || key != (iter->first))
1110+
{
1111+
throw web::json::json_exception(_XPLATSTR("Key not found"));
1112+
}
1113+
1114+
m_elements.erase(iter);
1115+
}
1116+
10591117
/// <summary>
10601118
/// Accesses an element of a JSON object. If the key doesn't exist, this method throws.
10611119
/// </summary>

Release/src/json/json.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,16 @@ bool json::value::operator==(const json::value &other) const
425425
__assume(0);
426426
}
427427

428+
void web::json::value::erase(size_t index)
429+
{
430+
return this->as_array().erase(index);
431+
}
432+
433+
void web::json::value::erase(const utility::string_t &key)
434+
{
435+
return this->as_object().erase(key);
436+
}
437+
428438
// at() overloads
429439
web::json::value& web::json::value::at(size_t index)
430440
{

Release/tests/functional/json/to_as_and_operators_tests.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,86 @@ TEST(negative_as_tests)
331331
VERIFY_THROWS(i.as_string(), json::json_exception);
332332
}
333333

334+
TEST(erase_array_index)
335+
{
336+
json::value a = json::value::array(4);
337+
a[0] = json::value(1);
338+
a[1] = json::value(2);
339+
a[2] = json::value(3);
340+
a[3] = json::value(4);
341+
342+
a.erase(1);
343+
VERIFY_ARE_EQUAL(3, a.size());
344+
VERIFY_ARE_EQUAL(1, a[0].as_integer());
345+
VERIFY_ARE_EQUAL(3, a[1].as_integer());
346+
VERIFY_ARE_EQUAL(4, a[2].as_integer());
347+
a.as_array().erase(2);
348+
VERIFY_ARE_EQUAL(2, a.size());
349+
VERIFY_ARE_EQUAL(1, a[0].as_integer());
350+
VERIFY_ARE_EQUAL(3, a[1].as_integer());
351+
}
352+
353+
TEST(erase_array_iter)
354+
{
355+
json::value a = json::value::array(3);
356+
a[0] = json::value(1);
357+
a[1] = json::value(2);
358+
a[2] = json::value(3);
359+
360+
auto iter = a.as_array().begin() + 1;
361+
a.as_array().erase(iter);
362+
VERIFY_ARE_EQUAL(2, a.size());
363+
VERIFY_ARE_EQUAL(1, a[0].as_integer());
364+
VERIFY_ARE_EQUAL(3, a[1].as_integer());
365+
366+
auto citer = a.as_array().cbegin() + 1;
367+
a.as_array().erase(citer);
368+
VERIFY_ARE_EQUAL(1, a.size());
369+
VERIFY_ARE_EQUAL(1, a[0].as_integer());
370+
}
371+
372+
TEST(erase_object_key)
373+
{
374+
auto o = json::value::object();
375+
o[U("a")] = json::value(1);
376+
o[U("b")] = json::value(2);
377+
o[U("c")] = json::value(3);
378+
o[U("d")] = json::value(4);
379+
380+
o.erase(U("a"));
381+
VERIFY_ARE_EQUAL(3, o.size());
382+
VERIFY_ARE_EQUAL(2, o[U("b")].as_integer());
383+
VERIFY_ARE_EQUAL(3, o[U("c")].as_integer());
384+
VERIFY_ARE_EQUAL(4, o[U("d")].as_integer());
385+
386+
o.as_object().erase(U("d"));
387+
VERIFY_ARE_EQUAL(2, o.size());
388+
VERIFY_ARE_EQUAL(2, o[U("b")].as_integer());
389+
VERIFY_ARE_EQUAL(3, o[U("c")].as_integer());
390+
}
391+
392+
TEST(erase_object_iter)
393+
{
394+
auto o = json::value::object();
395+
o[U("a")] = json::value(1);
396+
o[U("b")] = json::value(2);
397+
o[U("c")] = json::value(3);
398+
o[U("d")] = json::value(4);
399+
400+
auto iter = o.as_object().begin() + 1;
401+
o.as_object().erase(iter);
402+
VERIFY_ARE_EQUAL(3, o.size());
403+
VERIFY_ARE_EQUAL(1, o[U("a")].as_integer());
404+
VERIFY_ARE_EQUAL(3, o[U("c")].as_integer());
405+
VERIFY_ARE_EQUAL(4, o[U("d")].as_integer());
406+
407+
auto citer = o.as_object().begin() + 2;
408+
o.as_object().erase(citer);
409+
VERIFY_ARE_EQUAL(2, o.size());
410+
VERIFY_ARE_EQUAL(1, o[U("a")].as_integer());
411+
VERIFY_ARE_EQUAL(3, o[U("c")].as_integer());
412+
}
413+
334414
TEST(floating_number_serialize)
335415
{
336416
// This number will have the longest serializaton possible (lenght of the string):

0 commit comments

Comments
 (0)