10000 Added conversion from JsonArray/JsonObject to bool · joglosemarduino/ArduinoJson@7d1d0c4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7d1d0c4

Browse files
committed
Added conversion from JsonArray/JsonObject to bool
1 parent 4ad05db commit 7d1d0c4

File tree

5 files changed

+65
-8
lines changed

5 files changed

+65
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ HEAD
55
----
66

77
* Added support for custom writer classes (issue #1088)
8+
* Added conversion from `JsonArray` and `JsonObject` to `bool`, to be consistent with `JsonVariant`
89

910
v6.12.0 (2019-09-05)
1011
-------

extras/tests/JsonArray/isNull.cpp

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,53 @@
66
#include <catch.hpp>
77

88
TEST_CASE("JsonArray::isNull()") {
9-
DynamicJsonDocument doc(4096);
10-
119
SECTION("returns true") {
1210
JsonArray arr;
1311
REQUIRE(arr.isNull() == true);
1412
}
1513

1614
SECTION("returns false") {
15+
DynamicJsonDocument doc(4096);
1716
JsonArray arr = doc.to<JsonArray>();
1817
REQUIRE(arr.isNull() == false);
1918
}
2019
}
2120

2221
TEST_CASE("JsonArrayConst::isNull()") {
23-
DynamicJsonDocument doc(4096);
24-
2522
SECTION("returns true") {
2623
JsonArrayConst arr;
2724
REQUIRE(arr.isNull() == true);
2825
}
2926

3027
SECTION("returns false") {
28+
DynamicJsonDocument doc(4096);
3129
JsonArrayConst arr = doc.to<JsonArray>();
3230
REQUIRE(arr.isNull() == false);
3331
}
3432
}
33+
34+
TEST_CASE("JsonArray::operator bool()") {
35+
SECTION("returns false") {
36+
JsonArray arr;
37+
REQUIRE(static_cast<bool>(arr) == false);
38+
}
39+
40+
SECTION("returns true") {
41+
DynamicJsonDocument doc(4096);
42+
JsonArray arr = doc.to<JsonArray>();
43+
REQUIRE(static_cast<bool>(arr) == true);
44+
}
45+
}
46+
47+
TEST_CASE("JsonArrayConst::operator bool()") {
48+
SECTION("returns false") {
49+
JsonArrayConst arr;
50+
REQUIRE(static_cast<bool>(arr) == false);
51+
}
52+
53+
SECTION("returns true") {
54+
DynamicJsonDocument doc(4096);
55+
JsonArrayConst arr = doc.to<JsonArray>();
56+
REQUIRE(static_cast<bool>(arr) == true);
57+
}
58+
}

extras/tests/JsonObject/isNull.cpp

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,53 @@
66
#include <catch.hpp>
77

88
TEST_CASE("JsonObject::isNull()") {
9-
DynamicJsonDocument doc(4096);
10-
119
SECTION("returns true") {
1210
JsonObject obj;
1311
REQUIRE(obj.isNull() == true);
1412
}
1513

1614
SECTION("returns false") {
15+
DynamicJsonDocument doc(4096);
1716
JsonObject obj = doc.to<JsonObject>();
1817
REQUIRE(obj.isNull() == false);
1918
}
2019
}
2120

2221
TEST_CASE("JsonObjectConst::isNull()") {
23-
DynamicJsonDocument doc(4096);
24-
2522
SECTION("returns true") {
2623
JsonObjectConst obj;
2724
REQUIRE(obj.isNull() == true);
2825
}
2926

3027
SECTION("returns false") {
28+
DynamicJsonDocument doc(4096);
3129
JsonObjectConst obj = doc.to<JsonObject>();
3230
REQUIRE(obj.isNull() == false);
3331
}
3432
}
33+
34+
TEST_CASE("JsonObject::operator bool()") {
35+
SECTION("returns false") {
36+
JsonObject obj;
37+
REQUIRE(static_cast<bool>(obj) == false);
38+
}
39+
40+
SECTION("returns true") {
41+
DynamicJsonDocument doc(4096);
42+
JsonObject obj = doc.to<JsonObject>();
43+
REQUIRE(static_cast<bool>(obj) == true);
44+
}
45+
}
46+
47+
TEST_CASE("JsonObjectConst::operator bool()") {
48+
SECTION("returns false") {
49+
JsonObjectConst obj;
50+
REQUIRE(static_cast<bool>(obj) == false);
51+
}
52+
53+
SECTION("returns true") {
54+
DynamicJsonDocument doc(4096);
55+
JsonObjectConst obj = doc.to<JsonObject>();
56+
REQUIRE(static_cast<bool>(obj) == true);
57+
}
58+
}

src/ArduinoJson/Array/ArrayRef.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ class ArrayRefBase {
3636
return _data == 0;
3737
}
3838

39+
FORCE_INLINE operator bool() const {
40+
return _data != 0;
41+
}
42+
3943
FORCE_INLINE size_t memoryUsage() const {
4044
return _data ? _data->memoryUsage() : 0;
4145
}

src/ArduinoJson/Object/ObjectRef.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ class ObjectRefBase {
3131
return _data == 0;
3232
}
3333

34+
FORCE_INLINE operator bool() const {
35+
return _data != 0;
36+
}
37+
3438
FORCE_INLINE size_t memoryUsage() const {
3539
return _data ? _data->memoryUsage() : 0;
3640
}

0 commit comments

Comments
 (0)
0