diff --git a/CHANGELOG.md b/CHANGELOG.md index 9ebcdcf1..c140494c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ HEAD * CMake: don't build tests when imported in another project * CMake: made project arch-independent * Visual Studio: fixed error C2766 with flag `/Zc:__cplusplus` (issue #1250) +* Added support for `JsonDocument` to `copyArray()` (issue #1255) v6.15.1 (2020-04-08) ------- diff --git a/extras/tests/JsonArray/copyArray.cpp b/extras/tests/JsonArray/copyArray.cpp index 7e19eff4..7455f8fe 100644 --- a/extras/tests/JsonArray/copyArray.cpp +++ b/extras/tests/JsonArray/copyArray.cpp @@ -19,6 +19,18 @@ TEST_CASE("copyArray()") { REQUIRE(std::string("[1,2,3]") == json); } + SECTION("1D -> JsonDocument") { + DynamicJsonDocument doc(4096); + char json[32]; + int source[] = {1, 2, 3}; + + bool ok = copyArray(source, doc); + REQUIRE(ok); + + serializeJson(doc, json, sizeof(json)); + REQUIRE(std::string("[1,2,3]") == json); + } + SECTION("1D -> JsonArray, but not enough memory") { const size_t SIZE = JSON_ARRAY_SIZE(2); StaticJsonDocument doc; @@ -46,6 +58,18 @@ TEST_CASE("copyArray()") { REQUIRE(std::string("[[1,2,3],[4,5,6]]") == json); } + SECTION("2D -> JsonDocument") { + DynamicJsonDocument doc(4096); + char json[32]; + int source[][3] = {{1, 2, 3}, {4, 5, 6}}; + + bool ok = copyArray(source, doc); + REQUIRE(ok); + + serializeJson(doc, json, sizeof(json)); + REQUIRE(std::string("[[1,2,3],[4,5,6]]") == json); + } + SECTION("2D -> JsonArray, but not enough memory") { const size_t SIZE = JSON_ARRAY_SIZE(2) + JSON_ARRAY_SIZE(3) + JSON_ARRAY_SIZE(2); @@ -96,6 +120,22 @@ TEST_CASE("copyArray()") { REQUIRE(2 == destination[1]); } + SECTION("JsonDocument -> 1D") { + DynamicJsonDocument doc(4096); + char json[] = "[1,2,3]"; + DeserializationError err = deserializeJson(doc, json); + REQUIRE(err == DeserializationError::Ok); + + int destination[4] = {0}; + size_t result = copyArray(doc, destination); + + REQUIRE(3 == result); + REQUIRE(1 == destination[0]); + REQUIRE(2 == destination[1]); + REQUIRE(3 == destination[2]); + REQUIRE(0 == destination[3]); + } + SECTION("JsonArray -> 2D") { DynamicJsonDocument doc(4096); char json[] = "[[1,2],[3],[4]]"; @@ -114,4 +154,22 @@ TEST_CASE("copyArray()") { REQUIRE(4 == destination[2][0]); REQUIRE(0 == destination[2][1]); } + + SECTION("JsonDocument -> 2D") { + DynamicJsonDocument doc(4096); + char json[] = "[[1,2],[3],[4]]"; + + DeserializationError err = deserializeJson(doc, json); + REQUIRE(err == DeserializationError::Ok); + + int destination[3][2] = {{0}}; + copyArray(doc, destination); + + REQUIRE(1 == destination[0][0]); + REQUIRE(2 == destination[0][1]); + REQUIRE(3 == destination[1][0]); + REQUIRE(0 == destination[1][1]); + REQUIRE(4 == destination[2][0]); + REQUIRE(0 == destination[2][1]); + } } diff --git a/src/ArduinoJson/Array/Utilities.hpp b/src/ArduinoJson/Array/Utilities.hpp index c6b9d02a..a2895fd7 100644 --- a/src/ArduinoJson/Array/Utilities.hpp +++ b/src/ArduinoJson/Array/Utilities.hpp @@ -5,6 +5,7 @@ #pragma once #include +#include namespace ARDUINOJSON_NAMESPACE { @@ -14,6 +15,12 @@ inline bool copyArray(T (&src)[N], ArrayRef dst) { return copyArray(src, N, dst); } +// Copy a 1D array to a JsonDocument +template +inline bool copyArray(T (&src)[N], JsonDocument& dst) { + return copyArray(src, dst.to()); +} + // Copy a 1D array to a JsonArray template inline bool copyArray(T* src, size_t len, ArrayRef dst) { @@ -24,6 +31,12 @@ inline bool copyArray(T* src, size_t len, ArrayRef dst) { return ok; } +// Copy a 1D array to a JsonDocument +template +inline bool copyArray(T* src, size_t len, JsonDocument& dst) { + return copyArray(src, len, dst.to()); +} + // Copy a 2D array to a JsonArray template inline bool copyArray(T (&src)[N1][N2], ArrayRef dst) { @@ -37,12 +50,24 @@ inline bool copyArray(T (&src)[N1][N2], ArrayRef dst) { return ok; } +// Copy a 2D array to a JsonDocument +template +inline bool copyArray(T (&src)[N1][N2], JsonDocument& dst) { + return copyArray(src, dst.to()); +} + // Copy a JsonArray to a 1D array template inline size_t copyArray(ArrayConstRef src, T (&dst)[N]) { return copyArray(src, dst, N); } +// Copy a JsonDocument to a 1D array +template +inline size_t copyArray(const JsonDocument& src, T (&dst)[N]) { + return copyArray(src.as(), dst, N); +} + // Copy a JsonArray to a 1D array template inline size_t copyArray(ArrayConstRef src, T* dst, size_t len) { @@ -63,4 +88,10 @@ inline void copyArray(ArrayConstRef src, T (&dst)[N1][N2]) { } } +// Copy a JsonDocument to a 2D array +template +inline void copyArray(const JsonDocument& src, T (&dst)[N1][N2]) { + copyArray(src.as(), dst); +} + } // namespace ARDUINOJSON_NAMESPACE