mirror of
				https://github.com/eledio-devices/thirdparty-ArduinoJson.git
				synced 2025-10-31 16:14:11 +01:00 
			
		
		
		
	| @@ -9,6 +9,7 @@ HEAD | |||||||
| * Fixed `serializeJson(doc, String)` when allocation fails (issue #1572) | * Fixed `serializeJson(doc, String)` when allocation fails (issue #1572) | ||||||
| * Fixed clang-tidy warnings (issue #1574, PR #1577 by @armandas) | * Fixed clang-tidy warnings (issue #1574, PR #1577 by @armandas) | ||||||
| * Added fake class `InvalidConversion<T1,T2>` to easily identify invalid conversions (issue #1585) | * Added fake class `InvalidConversion<T1,T2>` to easily identify invalid conversions (issue #1585) | ||||||
|  | * Added support for `std::string_view` (issue #1578, PR #1554 by @0xFEEDC0DE64) | ||||||
|  |  | ||||||
| v6.18.0 (2021-05-05) | v6.18.0 (2021-05-05) | ||||||
| ------- | ------- | ||||||
|   | |||||||
| @@ -11,6 +11,7 @@ link_libraries(ArduinoJson catch) | |||||||
|  |  | ||||||
| include_directories(Helpers) | include_directories(Helpers) | ||||||
| add_subdirectory(Cpp11) | add_subdirectory(Cpp11) | ||||||
|  | add_subdirectory(Cpp17) | ||||||
| add_subdirectory(FailingBuilds) | add_subdirectory(FailingBuilds) | ||||||
| add_subdirectory(IntegrationTests) | add_subdirectory(IntegrationTests) | ||||||
| add_subdirectory(JsonArray) | add_subdirectory(JsonArray) | ||||||
|   | |||||||
							
								
								
									
										29
									
								
								extras/tests/Cpp17/CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								extras/tests/Cpp17/CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,29 @@ | |||||||
|  | # ArduinoJson - https://arduinojson.org | ||||||
|  | # Copyright Benoit Blanchon 2014-2021 | ||||||
|  | # MIT License | ||||||
|  |  | ||||||
|  | if(MSVC_VERSION LESS 1910) | ||||||
|  | 	return() | ||||||
|  | endif() | ||||||
|  |  | ||||||
|  | if(CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5) | ||||||
|  | 	return() | ||||||
|  | endif() | ||||||
|  |  | ||||||
|  | if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 7) | ||||||
|  | 	return() | ||||||
|  | endif() | ||||||
|  |  | ||||||
|  | set(CMAKE_CXX_STANDARD 17) | ||||||
|  | set(CMAKE_CXX_STANDARD_REQUIRED ON) | ||||||
|  |  | ||||||
|  | add_executable(Cpp17Tests | ||||||
|  | 	string_view.cpp | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | add_test(Cpp17 Cpp17Tests) | ||||||
|  |  | ||||||
|  | set_tests_properties(Cpp17 | ||||||
|  | 	PROPERTIES | ||||||
|  | 		LABELS 		"Catch" | ||||||
|  | ) | ||||||
							
								
								
									
										79
									
								
								extras/tests/Cpp17/string_view.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										79
									
								
								extras/tests/Cpp17/string_view.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,79 @@ | |||||||
|  | #include <ArduinoJson.h> | ||||||
|  |  | ||||||
|  | #include <catch.hpp> | ||||||
|  | #include <string_view> | ||||||
|  |  | ||||||
|  | #if !ARDUINOJSON_ENABLE_STRING_VIEW | ||||||
|  | #  error ARDUINOJSON_ENABLE_STRING_VIEW must be set to 1 | ||||||
|  | #endif | ||||||
|  |  | ||||||
|  | TEST_CASE("string_view") { | ||||||
|  |   StaticJsonDocument<128> doc; | ||||||
|  |   JsonVariant variant = doc.to<JsonVariant>(); | ||||||
|  |  | ||||||
|  |   SECTION("deserializeJson()") { | ||||||
|  |     auto err = deserializeJson(doc, std::string_view("123", 2)); | ||||||
|  |     REQUIRE(err == DeserializationError::Ok); | ||||||
|  |     REQUIRE(doc.as<int>() == 12); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   SECTION("JsonDocument::set()") { | ||||||
|  |     doc.set(std::string_view("123", 2)); | ||||||
|  |     REQUIRE(doc.as<std::string>() == "12"); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   SECTION("JsonDocument::operator[]() const") { | ||||||
|  |     doc["ab"] = "Yes"; | ||||||
|  |     doc["abc"] = "No"; | ||||||
|  |     REQUIRE(doc[std::string_view("abc", 2)] == "Yes"); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   SECTION("JsonDocument::operator[]()") { | ||||||
|  |     doc[std::string_view("abc", 2)] = "Yes"; | ||||||
|  |     REQUIRE(doc["ab"] == "Yes"); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   SECTION("JsonVariant::operator==()") { | ||||||
|  |     variant.set("A"); | ||||||
|  |     REQUIRE(variant == std::string_view("AX", 1)); | ||||||
|  |     REQUIRE_FALSE(variant == std::string_view("BX", 1)); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   SECTION("JsonVariant::operator>()") { | ||||||
|  |     variant.set("B"); | ||||||
|  |     REQUIRE(variant > std::string_view("AX", 1)); | ||||||
|  |     REQUIRE_FALSE(variant > std::string_view("CX", 1)); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   SECTION("JsonVariant::operator<()") { | ||||||
|  |     variant.set("B"); | ||||||
|  |     REQUIRE(variant < std::string_view("CX", 1)); | ||||||
|  |     REQUIRE_FALSE(variant < std::string_view("AX", 1)); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   SECTION("String deduplication") { | ||||||
|  |     doc.add(std::string_view("example one", 7)); | ||||||
|  |     REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(1) + 8); | ||||||
|  |  | ||||||
|  |     doc.add(std::string_view("example two", 7)); | ||||||
|  |     REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(2) + 8); | ||||||
|  |   } | ||||||
|  | } | ||||||
|  |  | ||||||
|  | using ARDUINOJSON_NAMESPACE::adaptString; | ||||||
|  |  | ||||||
|  | TEST_CASE("StringViewAdapter") { | ||||||
|  |   std::string_view str("bravoXXX", 5); | ||||||
|  |   auto adapter = adaptString(str); | ||||||
|  |  | ||||||
|  |   CHECK(adapter.compare(NULL) > 0); | ||||||
|  |   CHECK(adapter.compare("alpha") > 0); | ||||||
|  |   CHECK(adapter.compare("bravo") == 0); | ||||||
|  |   CHECK(adapter.compare("charlie") < 0); | ||||||
|  |  | ||||||
|  |   CHECK(adapter.equals("bravo")); | ||||||
|  |   CHECK_FALSE(adapter.equals("charlie")); | ||||||
|  |   CHECK_FALSE(adapter.equals(NULL)); | ||||||
|  |  | ||||||
|  |   CHECK(adapter.size() == 5); | ||||||
|  | } | ||||||
| @@ -62,6 +62,17 @@ | |||||||
| #  endif | #  endif | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
|  | #ifndef ARDUINOJSON_ENABLE_STRING_VIEW | ||||||
|  | #  ifdef __has_include | ||||||
|  | #    if __has_include(<string_view>) && __cplusplus >= 201703L | ||||||
|  | #      define ARDUINOJSON_ENABLE_STRING_VIEW 1 | ||||||
|  | #    endif | ||||||
|  | #  endif | ||||||
|  | #endif | ||||||
|  | #ifndef ARDUINOJSON_ENABLE_STRING_VIEW | ||||||
|  | #  define ARDUINOJSON_ENABLE_STRING_VIEW 0 | ||||||
|  | #endif | ||||||
|  |  | ||||||
| #if ARDUINOJSON_EMBEDDED_MODE | #if ARDUINOJSON_EMBEDDED_MODE | ||||||
|  |  | ||||||
| // Store floats by default to reduce the memory usage (issue #134) | // Store floats by default to reduce the memory usage (issue #134) | ||||||
|   | |||||||
| @@ -12,6 +12,10 @@ | |||||||
| #  include <ArduinoJson/Strings/StdStringAdapter.hpp> | #  include <ArduinoJson/Strings/StdStringAdapter.hpp> | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
|  | #if ARDUINOJSON_ENABLE_STRING_VIEW | ||||||
|  | #  include <ArduinoJson/Strings/StringViewAdapter.hpp> | ||||||
|  | #endif | ||||||
|  |  | ||||||
| #if ARDUINOJSON_ENABLE_ARDUINO_STRING | #if ARDUINOJSON_ENABLE_ARDUINO_STRING | ||||||
| #  include <ArduinoJson/Strings/ArduinoStringAdapter.hpp> | #  include <ArduinoJson/Strings/ArduinoStringAdapter.hpp> | ||||||
| #endif | #endif | ||||||
|   | |||||||
							
								
								
									
										56
									
								
								src/ArduinoJson/Strings/StringViewAdapter.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										56
									
								
								src/ArduinoJson/Strings/StringViewAdapter.hpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,56 @@ | |||||||
|  | // ArduinoJson - https://arduinojson.org | ||||||
|  | // Copyright Benoit Blanchon 2014-2021 | ||||||
|  | // MIT License | ||||||
|  |  | ||||||
|  | #pragma once | ||||||
|  |  | ||||||
|  | #include <ArduinoJson/Namespace.hpp> | ||||||
|  | #include <ArduinoJson/Strings/IsString.hpp> | ||||||
|  | #include <ArduinoJson/Strings/StoragePolicy.hpp> | ||||||
|  |  | ||||||
|  | #include <string_view> | ||||||
|  |  | ||||||
|  | namespace ARDUINOJSON_NAMESPACE { | ||||||
|  |  | ||||||
|  | class StringViewAdapter { | ||||||
|  |  public: | ||||||
|  |   StringViewAdapter(std::string_view str) : _str(str) {} | ||||||
|  |  | ||||||
|  |   void copyTo(char* p, size_t n) const { | ||||||
|  |     memcpy(p, _str.data(), n); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   bool isNull() const { | ||||||
|  |     return false; | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   int compare(const char* other) const { | ||||||
|  |     if (!other) | ||||||
|  |       return 1; | ||||||
|  |     return _str.compare(other); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   bool equals(const char* expected) const { | ||||||
|  |     if (!expected) | ||||||
|  |       return false; | ||||||
|  |     return _str == expected; | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   size_t size() const { | ||||||
|  |     return _str.size(); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   typedef storage_policies::store_by_copy storage_policy; | ||||||
|  |  | ||||||
|  |  private: | ||||||
|  |   std::string_view _str; | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | template <> | ||||||
|  | struct IsString<std::string_view> : true_type {}; | ||||||
|  |  | ||||||
|  | inline StringViewAdapter adaptString(const std::string_view& str) { | ||||||
|  |   return StringViewAdapter(str); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | }  // namespace ARDUINOJSON_NAMESPACE | ||||||
		Reference in New Issue
	
	Block a user