8000 Added `JsonVariant::clear()` · Cube-Line/ArduinoJson@bc2ce17 · GitHub
[go: up one dir, main page]

Skip to content

Commit bc2ce17

Browse files
committed
Added JsonVariant::clear()
1 parent e22e62d commit bc2ce17

File tree

10 files changed

+99
-0
lines changed

10 files changed

+99
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ HEAD
1717
* Fixed segfault after `variant.set(serialized((char*)0))`
1818
* Detect `IncompleteInput` in `false`, `true`, and `null`
1919
* Added `JsonDocument::size()`
20+
* Added `JsonVariant::clear()`
2021

2122
v6.8.0-beta (2019-01-30)
2223
-----------

src/ArduinoJson/Array/ElementProxy.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ class ElementProxy : public VariantOperators<ElementProxy<TArray> >,
4747
return *this;
4848
}
4949

50+
FORCE_INLINE void clear() const {
51+
getUpstreamElement().clear();
52+
}
53+
5054
FORCE_INLINE bool isNull() const {
5155
return getUpstreamElement().isNull();
5256
}

src/ArduinoJson/Object/MemberProxy.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ class MemberProxy : public VariantOperators<MemberProxy<TObject, TStringRef> >,
4949
return *this;
5050
}
5151

52+
FORCE_INLINE void clear() const {
53+
getUpstreamMember().clear();
54+
}
55+
5256
FORCE_INLINE bool isNull() const {
5357
return getUpstreamMember().isNull();
5458
}

src/ArduinoJson/Variant/VariantRef.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ class VariantRef : public VariantRefBase<VariantData>,
136136
// Creates an uninitialized VariantRef
137137
FORCE_INLINE VariantRef() : base_type(0), _pool(0) {}
138138

139+
FORCE_INLINE void clear() const {
140+
return variantSetNull(_data);
141+
}
142+
139143
// set(bool value)
140144
FORCE_INLINE bool set(bool value) const {
141145
return variantSetBoolean(_data, value);

test/ElementProxy/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
add_executable(ElementProxyTests
66
add.cpp
7+
clear.cpp
78
set.cpp
89
size.cpp
910
)

test/ElementProxy/clear.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// ArduinoJson - arduinojson.org
2+
// Copyright Benoit Blanchon 2014-2019
3+
// MIT License
4+
5+
#include <ArduinoJson.h>
6+
#include <catch.hpp>
7+
8+
using namespace ARDUINOJSON_NAMESPACE;
9+
10+
TEST_CASE("ElementProxy::clear()") {
11+
DynamicJsonDocument doc(4096);
12+
doc.addElement();
13+
ElementProxy<JsonDocument&> ep = doc[0];
14+
15+
SECTION("size goes back to zero") {
16+
ep.add(42);
17+
ep.clear();
18+
19+
REQUIRE(ep.size() == 0);
20+
}
21+
22+
SECTION("isNull() return true") {
23+
ep.add("hello");
24+
ep.clear();
25+
26+
REQUIRE(ep.isNull() == true);
27+
}
28+
}

test/JsonVariant/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
add_executable(JsonVariantTests
66
add.cpp
77
as.cpp
8+
clear.cpp
89
compare.cpp
910
copy.cpp
1011
createNested.cpp

test/JsonVariant/clear.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// ArduinoJson - arduinojson.org
2+
// Copyright Benoit Blanchon 2014-2019
3+
// MIT License
4+
5+
#include <ArduinoJson.h>
6+
#include <stdint.h>
7+
#include <catch.hpp>
8+
9+
static const char* null = 0;
10+
11+
TEST_CASE("JsonVariant::clear()") {
12+
DynamicJsonDocument doc(4096);
13+
JsonVariant var = doc.to<JsonVariant>();
14+
15+
SECTION("size goes back to zero") {
16+
var.add(42);
17+
var.clear();
18+
19+
REQUIRE(var.size() == 0);
20+
}
21+
22+
SECTION("isNull() return true") {
23+
var.add("hello");
24+
var.clear();
25+
26+
REQUIRE(var.isNull() == true);
27+
}
28+
}

test/MemberProxy/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
add_executable(MemberProxyTests
66
add.cpp
7+
clear.cpp
78
subscript.cpp
89
set.cpp
910
size.cpp

test/MemberProxy/clear.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// ArduinoJson - arduinojson.org
2+
// Copyright Benoit Blanchon 2014-2019
3+
// MIT License
4+
5+
#include <ArduinoJson.h>
6+
#include <catch.hpp>
7+
8+
using namespace ARDUINOJSON_NAMESPACE;
9+
10+
TEST_CASE("MemberProxy::clear()") {
11+
DynamicJsonDocument doc(4096);
12+
MemberProxy<JsonDocument&, const char*> mp = doc["hello"];
13+
14+
SECTION("size goes back to zero") {
15+
mp.add(42);
16+
mp.clear();
17+
18+
REQUIRE(mp.size() == 0);
19+
}
20+
21+
SECTION("isNull() return true") {
22+
mp.add("hello");
23+
mp.clear();
24+
25+
REQUIRE(mp.isNull() == true);
26+
}
27+
}

0 commit comments

Comments
 (0)
0