Added support for JsonDocument to copyArray() (issue #1255)

This commit is contained in:
Benoit Blanchon
2020-05-02 10:00:08 +02:00
parent 8050f7404b
commit 3b923b6e4e
3 changed files with 90 additions and 0 deletions

View File

@ -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<SIZE> 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]);
}
}