mirror of
				https://github.com/eledio-devices/thirdparty-ArduinoJson.git
				synced 2025-10-31 16:14:11 +01:00 
			
		
		
		
	Add implicit conversion from JsonDocument to JsonVariant
				
					
				
			This commit is contained in:
		| @@ -5,6 +5,7 @@ | ||||
| add_executable(JsonDocumentTests | ||||
| 	add.cpp | ||||
| 	BasicJsonDocument.cpp | ||||
| 	cast.cpp | ||||
| 	compare.cpp | ||||
| 	containsKey.cpp | ||||
| 	createNested.cpp | ||||
|   | ||||
| @@ -219,3 +219,29 @@ TEST_CASE("ElementProxy::operator[]") { | ||||
|     REQUIRE(doc.as<std::string>() == "[null,[null,null,42]]"); | ||||
|   } | ||||
| } | ||||
|  | ||||
| TEST_CASE("ElementProxy cast to JsonVariantConst") { | ||||
|   DynamicJsonDocument doc(4096); | ||||
|   doc[0] = "world"; | ||||
|  | ||||
|   const ElementProxy<JsonDocument &> ep = doc[0]; | ||||
|  | ||||
|   JsonVariantConst var = ep; | ||||
|  | ||||
|   CHECK(var.as<std::string>() == "world"); | ||||
| } | ||||
|  | ||||
| TEST_CASE("ElementProxy cast to JsonVariant") { | ||||
|   DynamicJsonDocument doc(4096); | ||||
|   doc[0] = "world"; | ||||
|  | ||||
|   ElementProxy<JsonDocument &> ep = doc[0]; | ||||
|  | ||||
|   JsonVariant var = ep; | ||||
|  | ||||
|   CHECK(var.as<std::string>() == "world"); | ||||
|  | ||||
|   var.set("toto"); | ||||
|  | ||||
|   CHECK(doc.as<std::string>() == "[\"toto\"]"); | ||||
| } | ||||
|   | ||||
| @@ -259,3 +259,29 @@ TEST_CASE("MemberProxy::operator[]") { | ||||
|     REQUIRE(doc.as<std::string>() == "{\"hello\":[null,null,42]}"); | ||||
|   } | ||||
| } | ||||
|  | ||||
| TEST_CASE("MemberProxy cast to JsonVariantConst") { | ||||
|   DynamicJsonDocument doc(4096); | ||||
|   doc["hello"] = "world"; | ||||
|  | ||||
|   const MemberProxy<JsonDocument &, const char *> mp = doc["hello"]; | ||||
|  | ||||
|   JsonVariantConst var = mp; | ||||
|  | ||||
|   CHECK(var.as<std::string>() == "world"); | ||||
| } | ||||
|  | ||||
| TEST_CASE("MemberProxy cast to JsonVariant") { | ||||
|   DynamicJsonDocument doc(4096); | ||||
|   doc["hello"] = "world"; | ||||
|  | ||||
|   MemberProxy<JsonDocument &, const char *> mp = doc["hello"]; | ||||
|  | ||||
|   JsonVariant var = mp; | ||||
|  | ||||
|   CHECK(var.as<std::string>() == "world"); | ||||
|  | ||||
|   var.set("toto"); | ||||
|  | ||||
|   CHECK(doc.as<std::string>() == "{\"hello\":\"toto\"}"); | ||||
| } | ||||
|   | ||||
							
								
								
									
										18
									
								
								extras/tests/JsonDocument/cast.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								extras/tests/JsonDocument/cast.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,18 @@ | ||||
| // ArduinoJson - https://arduinojson.org | ||||
| // Copyright © 2014-2022, Benoit BLANCHON | ||||
| // MIT License | ||||
|  | ||||
| #include <ArduinoJson.h> | ||||
| #include <catch.hpp> | ||||
|  | ||||
| #include <string> | ||||
|  | ||||
| TEST_CASE("Implicit cast to JsonVariant") { | ||||
|   StaticJsonDocument<128> doc; | ||||
|  | ||||
|   doc["hello"] = "world"; | ||||
|  | ||||
|   JsonVariant var = doc; | ||||
|  | ||||
|   CHECK(var.as<std::string>() == "{\"hello\":\"world\"}"); | ||||
| } | ||||
		Reference in New Issue
	
	Block a user