File tree Expand file tree Collapse file tree 3 files changed +57
-1
lines changed
src/ArduinoJson/Polyfills/type_traits Expand file tree Collapse file tree 3 files changed +57
-1
lines changed Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ add_executable(MiscTests
66 arithmeticCompare.cpp
77 conflicts.cpp
88 issue1967.cpp
9+ issue2129.cpp
910 JsonString.cpp
1011 NoArduinoHeader.cpp
1112 printable.cpp
Original file line number Diff line number Diff line change 1+ // ArduinoJson - https://arduinojson.org
2+ // Copyright © 2014-2024, Benoit BLANCHON
3+ // MIT License
4+
5+ #include < ArduinoJson.h>
6+ #include < catch.hpp>
7+
8+ template <typename T>
9+ class Nullable {
10+ public:
11+ Nullable () : value_{} {}
12+ Nullable (T value) : value_{value} {}
13+
14+ operator T () const {
15+ return value_;
16+ }
17+
18+ operator T&() {
19+ return value_;
20+ }
21+
22+ bool is_valid () const {
23+ return value_ != invalid_value_;
24+ }
25+
26+ T value () const {
27+ return value_;
28+ }
29+
30+ private:
31+ T value_;
32+ static T invalid_value_;
33+ };
34+
35+ template <>
36+ float Nullable<float >::invalid_value_ = std::numeric_limits<float >::lowest();
37+
38+ template <typename T>
39+ void convertToJson (const Nullable<T>& src, JsonVariant dst) {
40+ if (src.is_valid ()) {
41+ dst.set (src.value ());
42+ } else {
43+ dst.clear ();
44+ }
45+ }
46+
47+ TEST_CASE (" Issue #2129" ) {
48+ Nullable<float > nullable_value = Nullable<float >{123 .4f };
49+
50+ JsonDocument doc;
51+
52+ doc[" value" ] = nullable_value;
53+
54+ REQUIRE (doc[" value" ].as <float >() == Approx (123 .4f ));
55+ }
Original file line number Diff line number Diff line change @@ -27,7 +27,7 @@ struct is_convertible {
2727 static int probe (To);
2828 static char probe (...);
2929
30- static From& from_;
30+ static const From& from_;
3131
3232 public:
3333 static const bool value = sizeof (probe(from_)) == sizeof (int );
You can’t perform that action at this time.
0 commit comments