mirror of
				https://github.com/eledio-devices/thirdparty-ArduinoJson.git
				synced 2025-10-31 08:42:39 +01:00 
			
		
		
		
	Add support for char[][] in copyArray()
				
					
				
			This commit is contained in:
		| @@ -12,6 +12,7 @@ HEAD | ||||
| * Add safe bool idiom in `JsonString` | ||||
| * Add support for NUL in string values (issue #1646) | ||||
| * Add support for arbitrary array rank in `copyArray()` | ||||
| * Add support for `char[][]` in `copyArray()` | ||||
| * Remove `DeserializationError == bool` and `DeserializationError != bool` | ||||
| * Renamed undocumented function `isUndefined()` to `isUnbound()` | ||||
| * Fix `JsonVariant::memoryUsage()` for raw strings | ||||
|   | ||||
| @@ -32,6 +32,56 @@ TEST_CASE("copyArray()") { | ||||
|     CHECK(std::string("[\"a\",\"b\",\"c\"]") == json); | ||||
|   } | ||||
|  | ||||
|   SECTION("const char*[] -> JsonArray") { | ||||
|     DynamicJsonDocument doc(4096); | ||||
|     JsonArray array = doc.to<JsonArray>(); | ||||
|     char json[32]; | ||||
|     const char* source[] = {"a", "b", "c"}; | ||||
|  | ||||
|     bool ok = copyArray(source, array); | ||||
|     CHECK(ok); | ||||
|  | ||||
|     serializeJson(array, json); | ||||
|     CHECK(std::string("[\"a\",\"b\",\"c\"]") == json); | ||||
|   } | ||||
|  | ||||
|   SECTION("const char[][] -> JsonArray") { | ||||
|     DynamicJsonDocument doc(4096); | ||||
|     JsonArray array = doc.to<JsonArray>(); | ||||
|     char json[32]; | ||||
|     char source[][2] = {"a", "b", "c"}; | ||||
|  | ||||
|     bool ok = copyArray(source, array); | ||||
|     CHECK(ok); | ||||
|  | ||||
|     serializeJson(array, json); | ||||
|     CHECK(std::string("[\"a\",\"b\",\"c\"]") == json); | ||||
|   } | ||||
|  | ||||
|   SECTION("const char[][] -> JsonDocument") { | ||||
|     DynamicJsonDocument doc(4096); | ||||
|     char json[32]; | ||||
|     char source[][2] = {"a", "b", "c"}; | ||||
|  | ||||
|     bool ok = copyArray(source, doc); | ||||
|     CHECK(ok); | ||||
|  | ||||
|     serializeJson(doc, json); | ||||
|     CHECK(std::string("[\"a\",\"b\",\"c\"]") == json); | ||||
|   } | ||||
|  | ||||
|   SECTION("const char[][] -> MemberProxy") { | ||||
|     DynamicJsonDocument doc(4096); | ||||
|     char json[32]; | ||||
|     char source[][2] = {"a", "b", "c"}; | ||||
|  | ||||
|     bool ok = copyArray(source, doc["data"]); | ||||
|     CHECK(ok); | ||||
|  | ||||
|     serializeJson(doc, json); | ||||
|     CHECK(std::string("{\"data\":[\"a\",\"b\",\"c\"]}") == json); | ||||
|   } | ||||
|  | ||||
|   SECTION("int[] -> JsonDocument") { | ||||
|     DynamicJsonDocument doc(4096); | ||||
|     char json[32]; | ||||
| @@ -174,6 +224,23 @@ TEST_CASE("copyArray()") { | ||||
|     CHECK("" == destination[3]); | ||||
|   } | ||||
|  | ||||
|   SECTION("JsonArray -> char[N][]") { | ||||
|     DynamicJsonDocument doc(4096); | ||||
|     char json[] = "[\"a12345\",\"b123456\",\"c1234567\"]"; | ||||
|     DeserializationError err = deserializeJson(doc, json); | ||||
|     CHECK(err == DeserializationError::Ok); | ||||
|     JsonArray array = doc.as<JsonArray>(); | ||||
|  | ||||
|     char destination[4][8] = {{0}}; | ||||
|     size_t result = copyArray(array, destination); | ||||
|  | ||||
|     CHECK(3 == result); | ||||
|     CHECK(std::string("a12345") == destination[0]); | ||||
|     CHECK(std::string("b123456") == destination[1]); | ||||
|     CHECK(std::string("c123456") == destination[2]);  // truncated | ||||
|     CHECK(std::string("") == destination[3]); | ||||
|   } | ||||
|  | ||||
|   SECTION("JsonDocument -> int[]") { | ||||
|     DynamicJsonDocument doc(4096); | ||||
|     char json[] = "[1,2,3]"; | ||||
|   | ||||
| @@ -36,6 +36,12 @@ copyArray(const T* src, size_t len, const TDestination& dst) { | ||||
|   return ok; | ||||
| } | ||||
|  | ||||
| // Special case for char[] which much be treated as const char* | ||||
| template <typename TDestination> | ||||
| inline bool copyArray(const char* src, size_t, const TDestination& dst) { | ||||
|   return dst.set(src); | ||||
| } | ||||
|  | ||||
| // Copy array to a JsonDocument | ||||
| template <typename T> | ||||
| inline bool copyArray(const T& src, JsonDocument& dst) { | ||||
| @@ -72,6 +78,18 @@ inline size_t copyArray(ArrayConstRef src, T* dst, size_t len) { | ||||
|   return i; | ||||
| } | ||||
|  | ||||
| // Special case for char[] which must be treated as a string | ||||
| template <size_t N> | ||||
| inline size_t copyArray(VariantConstRef src, char (&dst)[N]) { | ||||
|   String s = src; | ||||
|   size_t len = N - 1; | ||||
|   if (len > s.size()) | ||||
|     len = s.size(); | ||||
|   memcpy(dst, s.c_str(), len); | ||||
|   dst[len] = 0; | ||||
|   return 1; | ||||
| } | ||||
|  | ||||
| // Copy a JsonDocument to an array | ||||
| // (JsonDocument doesn't implicitly convert to JsonArrayConst) | ||||
| template <typename TSource, typename T> | ||||
|   | ||||
		Reference in New Issue
	
	Block a user