Skip to content

Commit 2ed77d2

Browse files
committed
Added support for nullptr (closes #998)
1 parent 630107a commit 2ed77d2

File tree

5 files changed

+58
-2
lines changed

5 files changed

+58
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ HEAD
1111
* Added `ARDUINOJSON_ENABLE_INFINITY` (default=0) to enable Infinity in JSON
1212
* Removed implicit conversion in comparison operators (issue #998)
1313
* Added lexicographical comparison for `JsonVariant`
14+
* Added support for `nullptr` (issue #998)
1415

1516
> ### BREAKING CHANGES
1617
>

src/ArduinoJson/Configuration.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212

1313
#if __cplusplus >= 201103L
1414
#define ARDUINOJSON_HAS_LONG_LONG 1
15+
#define ARDUINOJSON_HAS_NULLPTR 1
1516
#else
1617
#define ARDUINOJSON_HAS_LONG_LONG 0
18+
#define ARDUINOJSON_HAS_NULLPTR 0
1719
#endif
1820

1921
// Small or big machine?

src/ArduinoJson/Operators/VariantComparisons.hpp

+21
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,27 @@ struct Comparer<bool, void> {
8888
void visitNull() {}
8989
};
9090

91+
#if ARDUINOJSON_HAS_NULLPTR
92+
template <>
93+
struct Comparer<decltype(nullptr), void> {
94+
int result;
95+
96+
explicit Comparer(decltype(nullptr)) : result(1) {}
97+
98+
void visitArray(const CollectionData &) {}
99+
void visitObject(const CollectionData &) {}
100+
void visitFloat(Float) {}
101+
void visitString(const char *) {}
102+
void visitRawJson(const char *, size_t) {}
103+
void visitNegativeInteger(UInt) {}
104+
void visitPositiveInteger(UInt) {}
105+
void visitBoolean(bool) {}
106+
void visitNull() {
107+
result = 0;
108+
}
109+
};
110+
#endif
111+
91112
template <typename TVariant>
92113
class VariantComparisons {
93114
private:

test/MixedConfiguration/CMakeLists.txt

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
set(CMAKE_CXX_STANDARD 11)
77

88
add_executable(MixedConfigurationTests
9+
cpp11.cpp
910
decode_unicode_0.cpp
1011
decode_unicode_1.cpp
11-
enable_nan_0.cpp
12-
enable_nan_1.cpp
1312
enable_infinity_0.cpp
1413
enable_infinity_1.cpp
14+
enable_nan_0.cpp
15+
enable_nan_1.cpp
1516
use_double_0.cpp
1617
use_double_1.cpp
1718
use_long_long_0.cpp

test/MixedConfiguration/cpp11.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <ArduinoJson.h>
2+
3+
#include <catch.hpp>
4+
5+
#if __cplusplus >= 201103L
6+
7+
TEST_CASE("nullptr") {
8+
DynamicJsonDocument doc(4096);
9+
JsonVariant variant = doc.to<JsonVariant>();
10+
11+
SECTION("JsonVariant == nullptr") {
12+
REQUIRE((variant == nullptr));
13+
REQUIRE_FALSE((variant != nullptr));
14+
}
15+
16+
SECTION("JsonVariant != nullptr") {
17+
variant.set(42);
18+
19+
REQUIRE_FALSE((variant == nullptr));
20+
REQUIRE((variant != nullptr));
21+
}
22+
23+
SECTION("JsonVariant.set(nullptr)") {
24+
variant.set(42);
25+
variant.set(nullptr);
26+
27+
REQUIRE(variant.isNull());
28+
}
29+
}
30+
31+
#endif

0 commit comments

Comments
 (0)