diff --git a/CHANGELOG.md b/CHANGELOG.md index 90a61303..1eeccc4b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/extras/tests/JsonArray/copyArray.cpp b/extras/tests/JsonArray/copyArray.cpp index 3f5f47c2..fadd4b43 100644 --- a/extras/tests/JsonArray/copyArray.cpp +++ b/extras/tests/JsonArray/copyArray.cpp @@ -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(); + 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(); + 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(); + + 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]"; diff --git a/src/ArduinoJson/Array/Utilities.hpp b/src/ArduinoJson/Array/Utilities.hpp index 5d7f5a04..fc56b449 100644 --- a/src/ArduinoJson/Array/Utilities.hpp +++ b/src/ArduinoJson/Array/Utilities.hpp @@ -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 +inline bool copyArray(const char* src, size_t, const TDestination& dst) { + return dst.set(src); +} + // Copy array to a JsonDocument template 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 +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