Skip to content

Commit a6fc931

Browse files
committed
Added JsonArray::clear() (fixes #1597)
1 parent 14639f1 commit a6fc931

File tree

6 files changed

+59
-0
lines changed

6 files changed

+59
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ HEAD
1111
* Added fake class `InvalidConversion<T1,T2>` to easily identify invalid conversions (issue #1585)
1212
* Added support for `std::string_view` (issue #1578, PR #1554 by @0xFEEDC0DE64)
1313
* Fixed warning `definition of implicit copy constructor for 'MsgPackDeserializer' is deprecated because it has a user-declared copy assignment operator`
14+
* Added `JsonArray::clear()` (issue #1597)
1415

1516
v6.18.0 (2021-05-05)
1617
-------

extras/tests/JsonArray/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
add_executable(JsonArrayTests
66
add.cpp
7+
clear.cpp
78
copyArray.cpp
89
createNested.cpp
910
equals.cpp

extras/tests/JsonArray/clear.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// ArduinoJson - https://arduinojson.org
2+
// Copyright Benoit Blanchon 2014-2021
3+
// MIT License
4+
5+
#include <ArduinoJson.h>
6+
#include <catch.hpp>
7+
8+
TEST_CASE("JsonArray::clear()") {
9+
SECTION("No-op on null JsonArray") {
10+
JsonArray array;
11+
array.clear();
12+
REQUIRE(array.isNull() == true);
13+
REQUIRE(array.size() == 0);
14+
}
15+
16+
SECTION("Removes all elements") {
17+
StaticJsonDocument<64> doc;
18+
JsonArray array = doc.to<JsonArray>();
19+
array.add(1);
20+
array.add(2);
21+
array.clear();
22+
REQUIRE(array.size() == 0);
23+
REQUIRE(array.isNull() == false);
24+
}
25+
}

extras/tests/JsonObject/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# MIT License
44

55
add_executable(JsonObjectTests
6+
clear.cpp
67
containsKey.cpp
78
copy.cpp
89
createNestedArray.cpp

extras/tests/JsonObject/clear.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// ArduinoJson - https://arduinojson.org
2+
// Copyright Benoit Blanchon 2014-2021
3+
// MIT License
4+
5+
#include <ArduinoJson.h>
6+
#include <catch.hpp>
7+
8+
TEST_CASE("JsonObject::clear()") {
9+
SECTION("No-op on null JsonObject") {
10+
JsonObject obj;
11+
obj.clear();
12+
REQUIRE(obj.isNull() == true);
13+
REQUIRE(obj.size() == 0);
14+
}
15+
16+
SECTION("Removes all elements") {
17+
StaticJsonDocument<64> doc;
18+
JsonObject obj = doc.to<JsonObject>();
19+
obj["hello"] = 1;
20+
obj["world"] = 2;
21+
obj.clear();
22+
REQUIRE(obj.size() == 0);
23+
REQUIRE(obj.isNull() == false);
24+
}
25+
}

src/ArduinoJson/Array/ArrayRef.hpp

+6
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,12 @@ class ArrayRef : public ArrayRefBase<CollectionData>,
161161
_data->removeElement(index);
162162
}
163163

164+
void clear() const {
165+
if (!_data)
166+
return;
167+
_data->clear();
168+
}
169+
164170
private:
165171
MemoryPool* _pool;
166172
};

0 commit comments

Comments
 (0)