Add support for char[][] in copyArray()

This commit is contained in:
Benoit Blanchon
2021-12-20 13:18:31 +01:00
parent 184a7c4fbd
commit ada1f2a894
3 changed files with 86 additions and 0 deletions

View File

@ -12,6 +12,7 @@ HEAD
* Add safe bool idiom in `JsonString` * Add safe bool idiom in `JsonString`
* Add support for NUL in string values (issue #1646) * Add support for NUL in string values (issue #1646)
* Add support for arbitrary array rank in `copyArray()` * Add support for arbitrary array rank in `copyArray()`
* Add support for `char[][]` in `copyArray()`
* Remove `DeserializationError == bool` and `DeserializationError != bool` * Remove `DeserializationError == bool` and `DeserializationError != bool`
* Renamed undocumented function `isUndefined()` to `isUnbound()` * Renamed undocumented function `isUndefined()` to `isUnbound()`
* Fix `JsonVariant::memoryUsage()` for raw strings * Fix `JsonVariant::memoryUsage()` for raw strings

View File

@ -32,6 +32,56 @@ TEST_CASE("copyArray()") {
CHECK(std::string("[\"a\",\"b\",\"c\"]") == json); 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") { SECTION("int[] -> JsonDocument") {
DynamicJsonDocument doc(4096); DynamicJsonDocument doc(4096);
char json[32]; char json[32];
@ -174,6 +224,23 @@ TEST_CASE("copyArray()") {
CHECK("" == destination[3]); 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[]") { SECTION("JsonDocument -> int[]") {
DynamicJsonDocument doc(4096); DynamicJsonDocument doc(4096);
char json[] = "[1,2,3]"; char json[] = "[1,2,3]";

View File

@ -36,6 +36,12 @@ copyArray(const T* src, size_t len, const TDestination& dst) {
return ok; 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 // Copy array to a JsonDocument
template <typename T> template <typename T>
inline bool copyArray(const T& src, JsonDocument& dst) { 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; 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 // Copy a JsonDocument to an array
// (JsonDocument doesn't implicitly convert to JsonArrayConst) // (JsonDocument doesn't implicitly convert to JsonArrayConst)
template <typename TSource, typename T> template <typename TSource, typename T>