diff --git a/CHANGELOG.md b/CHANGELOG.md index 264a3c8f..e948191b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ HEAD * Added comparisons (`>`, `>=`, `==`, `!=`, `<`, and `<=`) between `JsonVariant`s * Added string deduplication (issue #1303) +* Fixed `copyArray()` not working with `String` v6.15.2 (2020-05-15) ------- diff --git a/extras/tests/JsonArray/copyArray.cpp b/extras/tests/JsonArray/copyArray.cpp index 7455f8fe..a85647e9 100644 --- a/extras/tests/JsonArray/copyArray.cpp +++ b/extras/tests/JsonArray/copyArray.cpp @@ -6,7 +6,7 @@ #include TEST_CASE("copyArray()") { - SECTION("1D -> JsonArray") { + SECTION("int[] -> JsonArray") { DynamicJsonDocument doc(4096); JsonArray array = doc.to(); char json[32]; @@ -19,7 +19,20 @@ TEST_CASE("copyArray()") { REQUIRE(std::string("[1,2,3]") == json); } - SECTION("1D -> JsonDocument") { + SECTION("std::string[] -> JsonArray") { + DynamicJsonDocument doc(4096); + JsonArray array = doc.to(); + char json[32]; + std::string source[] = {"a", "b", "c"}; + + bool ok = copyArray(source, array); + REQUIRE(ok); + + serializeJson(array, json, sizeof(json)); + REQUIRE(std::string("[\"a\",\"b\",\"c\"]") == json); + } + + SECTION("int[] -> JsonDocument") { DynamicJsonDocument doc(4096); char json[32]; int source[] = {1, 2, 3}; @@ -31,7 +44,7 @@ TEST_CASE("copyArray()") { REQUIRE(std::string("[1,2,3]") == json); } - SECTION("1D -> JsonArray, but not enough memory") { + SECTION("int[] -> JsonArray, but not enough memory") { const size_t SIZE = JSON_ARRAY_SIZE(2); StaticJsonDocument doc; JsonArray array = doc.to(); @@ -45,7 +58,7 @@ TEST_CASE("copyArray()") { REQUIRE(std::string("[1,2]") == json); } - SECTION("2D -> JsonArray") { + SECTION("int[][] -> JsonArray") { DynamicJsonDocument doc(4096); JsonArray array = doc.to(); char json[32]; @@ -58,7 +71,7 @@ TEST_CASE("copyArray()") { REQUIRE(std::string("[[1,2,3],[4,5,6]]") == json); } - SECTION("2D -> JsonDocument") { + SECTION("int[][] -> JsonDocument") { DynamicJsonDocument doc(4096); char json[32]; int source[][3] = {{1, 2, 3}, {4, 5, 6}}; @@ -70,7 +83,7 @@ TEST_CASE("copyArray()") { REQUIRE(std::string("[[1,2,3],[4,5,6]]") == json); } - SECTION("2D -> JsonArray, but not enough memory") { + SECTION("int[][] -> JsonArray, but not enough memory") { const size_t SIZE = JSON_ARRAY_SIZE(2) + JSON_ARRAY_SIZE(3) + JSON_ARRAY_SIZE(2); StaticJsonDocument doc; @@ -88,7 +101,7 @@ TEST_CASE("copyArray()") { REQUIRE(std::string("[[1,2,3],[4,5]]") == json); } - SECTION("JsonArray -> 1D, with more space than needed") { + SECTION("JsonArray -> int[], with more space than needed") { DynamicJsonDocument doc(4096); char json[] = "[1,2,3]"; DeserializationError err = deserializeJson(doc, json); @@ -105,7 +118,7 @@ TEST_CASE("copyArray()") { REQUIRE(0 == destination[3]); } - SECTION("JsonArray -> 1D, without enough space") { + SECTION("JsonArray -> int[], without enough space") { DynamicJsonDocument doc(4096); char json[] = "[1,2,3]"; DeserializationError err = deserializeJson(doc, json); @@ -120,7 +133,24 @@ TEST_CASE("copyArray()") { REQUIRE(2 == destination[1]); } - SECTION("JsonDocument -> 1D") { + SECTION("JsonArray -> std::string[]") { + DynamicJsonDocument doc(4096); + char json[] = "[\"a\",\"b\",\"c\"]"; + DeserializationError err = deserializeJson(doc, json); + REQUIRE(err == DeserializationError::Ok); + JsonArray array = doc.as(); + + std::string destination[4]; + size_t result = copyArray(array, destination); + + REQUIRE(3 == result); + CHECK("a" == destination[0]); + CHECK("b" == destination[1]); + CHECK("c" == destination[2]); + CHECK("" == destination[3]); + } + + SECTION("JsonDocument -> int[]") { DynamicJsonDocument doc(4096); char json[] = "[1,2,3]"; DeserializationError err = deserializeJson(doc, json); @@ -136,7 +166,7 @@ TEST_CASE("copyArray()") { REQUIRE(0 == destination[3]); } - SECTION("JsonArray -> 2D") { + SECTION("JsonArray -> int[][]") { DynamicJsonDocument doc(4096); char json[] = "[[1,2],[3],[4]]"; @@ -155,7 +185,7 @@ TEST_CASE("copyArray()") { REQUIRE(0 == destination[2][1]); } - SECTION("JsonDocument -> 2D") { + SECTION("JsonDocument -> int[][]") { DynamicJsonDocument doc(4096); char json[] = "[[1,2],[3],[4]]"; diff --git a/src/ArduinoJson/Array/Utilities.hpp b/src/ArduinoJson/Array/Utilities.hpp index a2895fd7..0a3477a7 100644 --- a/src/ArduinoJson/Array/Utilities.hpp +++ b/src/ArduinoJson/Array/Utilities.hpp @@ -74,7 +74,7 @@ inline size_t copyArray(ArrayConstRef src, T* dst, size_t len) { size_t i = 0; for (ArrayConstRef::iterator it = src.begin(); it != src.end() && i < len; ++it) - dst[i++] = *it; + dst[i++] = it->as(); return i; }