mirror of
				https://github.com/eledio-devices/thirdparty-ArduinoJson.git
				synced 2025-10-31 16:14:11 +01:00 
			
		
		
		
	Added as<std::string_view>() and is<std::string_view>()
				
					
				
			This commit is contained in:
		| @@ -5,6 +5,7 @@ HEAD | |||||||
| ---- | ---- | ||||||
|  |  | ||||||
| * Changed return type of `convertToJson()` and `Converter<T>::toJson()` to `void` | * Changed return type of `convertToJson()` and `Converter<T>::toJson()` to `void` | ||||||
|  | * Added `as<std::string_view>()` and `is<std::string_view>()` | ||||||
|  |  | ||||||
| v6.18.2 (2021-07-19) | v6.18.2 (2021-07-19) | ||||||
| ------- | ------- | ||||||
|   | |||||||
| @@ -58,6 +58,20 @@ TEST_CASE("string_view") { | |||||||
|     doc.add(std::string_view("example two", 7)); |     doc.add(std::string_view("example two", 7)); | ||||||
|     REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(2) + 8); |     REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(2) + 8); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  |   SECTION("as<std::string_view>()") { | ||||||
|  |     doc["s"] = "Hello World"; | ||||||
|  |     doc["i"] = 42; | ||||||
|  |     REQUIRE(doc["s"].as<std::string_view>() == std::string_view("Hello World")); | ||||||
|  |     REQUIRE(doc["i"].as<std::string_view>() == std::string_view()); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   SECTION("is<std::string_view>()") { | ||||||
|  |     doc["s"] = "Hello World"; | ||||||
|  |     doc["i"] = 42; | ||||||
|  |     REQUIRE(doc["s"].is<std::string_view>() == true); | ||||||
|  |     REQUIRE(doc["i"].is<std::string_view>() == false); | ||||||
|  |   } | ||||||
| } | } | ||||||
|  |  | ||||||
| using ARDUINOJSON_NAMESPACE::adaptString; | using ARDUINOJSON_NAMESPACE::adaptString; | ||||||
|   | |||||||
| @@ -256,4 +256,18 @@ inline void convertToJson(const ::Printable& src, VariantRef dst) { | |||||||
|  |  | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
|  | #if ARDUINOJSON_ENABLE_STRING_VIEW | ||||||
|  |  | ||||||
|  | inline void convertFromJson(VariantConstRef src, std::string_view& dst) { | ||||||
|  |   const char* str = src.as<const char*>(); | ||||||
|  |   if (str)  // the standard doesn't allow passing null to the constructor | ||||||
|  |     dst = std::string_view(str); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | inline bool canConvertFromJson(VariantConstRef src, const std::string_view&) { | ||||||
|  |   return src.is<const char*>(); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | #endif | ||||||
|  |  | ||||||
| }  // namespace ARDUINOJSON_NAMESPACE | }  // namespace ARDUINOJSON_NAMESPACE | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user