File tree Expand file tree Collapse file tree 6 files changed +78
-9
lines changed
Expand file tree Collapse file tree 6 files changed +78
-9
lines changed Original file line number Diff line number Diff line change 66
77* Add ` ElementProxy::memoryUsage() `
88* Add ` MemberProxy::memoryUsage() ` (issue #1730 )
9+ * Add implicit conversion from ` JsonDocument ` to ` JsonVariant `
910
1011v6.19.3 (2022-03-08)
1112-------
Original file line number Diff line number Diff line change 55add_executable (JsonDocumentTests
66 add.cpp
77 BasicJsonDocument.cpp
8+ cast.cpp
89 compare.cpp
910 containsKey.cpp
1011 createNested.cpp
Original file line number Diff line number Diff line change @@ -219,3 +219,29 @@ TEST_CASE("ElementProxy::operator[]") {
219219 REQUIRE (doc.as <std::string>() == " [null,[null,null,42]]" );
220220 }
221221}
222+
223+ TEST_CASE (" ElementProxy cast to JsonVariantConst" ) {
224+ DynamicJsonDocument doc (4096 );
225+ doc[0 ] = " world" ;
226+
227+ const ElementProxy<JsonDocument &> ep = doc[0 ];
228+
229+ JsonVariantConst var = ep;
230+
231+ CHECK (var.as <std::string>() == " world" );
232+ }
233+
234+ TEST_CASE (" ElementProxy cast to JsonVariant" ) {
235+ DynamicJsonDocument doc (4096 );
236+ doc[0 ] = " world" ;
237+
238+ ElementProxy<JsonDocument &> ep = doc[0 ];
239+
240+ JsonVariant var = ep;
241+
242+ CHECK (var.as <std::string>() == " world" );
243+
244+ var.set (" toto" );
245+
246+ CHECK (doc.as <std::string>() == " [\" toto\" ]" );
247+ }
Original file line number Diff line number Diff line change @@ -259,3 +259,29 @@ TEST_CASE("MemberProxy::operator[]") {
259259 REQUIRE (doc.as <std::string>() == " {\" hello\" :[null,null,42]}" );
260260 }
261261}
262+
263+ TEST_CASE (" MemberProxy cast to JsonVariantConst" ) {
264+ DynamicJsonDocument doc (4096 );
265+ doc[" hello" ] = " world" ;
266+
267+ const MemberProxy<JsonDocument &, const char *> mp = doc[" hello" ];
268+
269+ JsonVariantConst var = mp;
270+
271+ CHECK (var.as <std::string>() == " world" );
272+ }
273+
274+ TEST_CASE (" MemberProxy cast to JsonVariant" ) {
275+ DynamicJsonDocument doc (4096 );
276+ doc[" hello" ] = " world" ;
277+
278+ MemberProxy<JsonDocument &, const char *> mp = doc[" hello" ];
279+
280+ JsonVariant var = mp;
281+
282+ CHECK (var.as <std::string>() == " world" );
283+
284+ var.set (" toto" );
285+
286+ CHECK (doc.as <std::string>() == " {\" hello\" :\" toto\" }" );
287+ }
Original file line number Diff line number Diff line change 1+ // ArduinoJson - https://arduinojson.org
2+ // Copyright © 2014-2022, Benoit BLANCHON
3+ // MIT License
4+
5+ #include < ArduinoJson.h>
6+ #include < catch.hpp>
7+
8+ #include < string>
9+
10+ TEST_CASE (" Implicit cast to JsonVariant" ) {
11+ StaticJsonDocument<128 > doc;
12+
13+ doc[" hello" ] = " world" ;
14+
15+ JsonVariant var = doc;
16+
17+ CHECK (var.as <std::string>() == " {\" hello\" :\" world\" }" );
18+ }
Original file line number Diff line number Diff line change 1414
1515namespace ARDUINOJSON_NAMESPACE {
1616
17- class JsonDocument : public Visitable {
17+ class JsonDocument : public Visitable ,
18+ public VariantOperators<const JsonDocument&> {
1819 public:
1920 template <typename TVisitor>
2021 typename TVisitor::result_type accept (TVisitor& visitor) const {
@@ -295,16 +296,12 @@ class JsonDocument : public Visitable {
295296 _data.remove (adaptString (key));
296297 }
297298
298- FORCE_INLINE operator VariantConstRef () const {
299- return VariantConstRef (&_data );
299+ FORCE_INLINE operator VariantRef () {
300+ return getVariant ( );
300301 }
301302
302- bool operator ==(VariantConstRef rhs) const {
303- return getVariant () == rhs;
304- }
305-
306- bool operator !=(VariantConstRef rhs) const {
307- return getVariant () != rhs;
303+ FORCE_INLINE operator VariantConstRef () const {
304+ return getVariant ();
308305 }
309306
310307 protected:
You can’t perform that action at this time.
0 commit comments