mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-16 20:12:16 +02:00
Fixed copyArray()
not working with String
This commit is contained in:
@ -6,6 +6,7 @@ HEAD
|
|||||||
|
|
||||||
* Added comparisons (`>`, `>=`, `==`, `!=`, `<`, and `<=`) between `JsonVariant`s
|
* Added comparisons (`>`, `>=`, `==`, `!=`, `<`, and `<=`) between `JsonVariant`s
|
||||||
* Added string deduplication (issue #1303)
|
* Added string deduplication (issue #1303)
|
||||||
|
* Fixed `copyArray()` not working with `String`
|
||||||
|
|
||||||
v6.15.2 (2020-05-15)
|
v6.15.2 (2020-05-15)
|
||||||
-------
|
-------
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
#include <catch.hpp>
|
#include <catch.hpp>
|
||||||
|
|
||||||
TEST_CASE("copyArray()") {
|
TEST_CASE("copyArray()") {
|
||||||
SECTION("1D -> JsonArray") {
|
SECTION("int[] -> JsonArray") {
|
||||||
DynamicJsonDocument doc(4096);
|
DynamicJsonDocument doc(4096);
|
||||||
JsonArray array = doc.to<JsonArray>();
|
JsonArray array = doc.to<JsonArray>();
|
||||||
char json[32];
|
char json[32];
|
||||||
@ -19,7 +19,20 @@ TEST_CASE("copyArray()") {
|
|||||||
REQUIRE(std::string("[1,2,3]") == json);
|
REQUIRE(std::string("[1,2,3]") == json);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("1D -> JsonDocument") {
|
SECTION("std::string[] -> JsonArray") {
|
||||||
|
DynamicJsonDocument doc(4096);
|
||||||
|
JsonArray array = doc.to<JsonArray>();
|
||||||
|
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);
|
DynamicJsonDocument doc(4096);
|
||||||
char json[32];
|
char json[32];
|
||||||
int source[] = {1, 2, 3};
|
int source[] = {1, 2, 3};
|
||||||
@ -31,7 +44,7 @@ TEST_CASE("copyArray()") {
|
|||||||
REQUIRE(std::string("[1,2,3]") == json);
|
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);
|
const size_t SIZE = JSON_ARRAY_SIZE(2);
|
||||||
StaticJsonDocument<SIZE> doc;
|
StaticJsonDocument<SIZE> doc;
|
||||||
JsonArray array = doc.to<JsonArray>();
|
JsonArray array = doc.to<JsonArray>();
|
||||||
@ -45,7 +58,7 @@ TEST_CASE("copyArray()") {
|
|||||||
REQUIRE(std::string("[1,2]") == json);
|
REQUIRE(std::string("[1,2]") == json);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("2D -> JsonArray") {
|
SECTION("int[][] -> JsonArray") {
|
||||||
DynamicJsonDocument doc(4096);
|
DynamicJsonDocument doc(4096);
|
||||||
JsonArray array = doc.to<JsonArray>();
|
JsonArray array = doc.to<JsonArray>();
|
||||||
char json[32];
|
char json[32];
|
||||||
@ -58,7 +71,7 @@ TEST_CASE("copyArray()") {
|
|||||||
REQUIRE(std::string("[[1,2,3],[4,5,6]]") == json);
|
REQUIRE(std::string("[[1,2,3],[4,5,6]]") == json);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("2D -> JsonDocument") {
|
SECTION("int[][] -> JsonDocument") {
|
||||||
DynamicJsonDocument doc(4096);
|
DynamicJsonDocument doc(4096);
|
||||||
char json[32];
|
char json[32];
|
||||||
int source[][3] = {{1, 2, 3}, {4, 5, 6}};
|
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);
|
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 =
|
const size_t SIZE =
|
||||||
JSON_ARRAY_SIZE(2) + JSON_ARRAY_SIZE(3) + JSON_ARRAY_SIZE(2);
|
JSON_ARRAY_SIZE(2) + JSON_ARRAY_SIZE(3) + JSON_ARRAY_SIZE(2);
|
||||||
StaticJsonDocument<SIZE> doc;
|
StaticJsonDocument<SIZE> doc;
|
||||||
@ -88,7 +101,7 @@ TEST_CASE("copyArray()") {
|
|||||||
REQUIRE(std::string("[[1,2,3],[4,5]]") == json);
|
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);
|
DynamicJsonDocument doc(4096);
|
||||||
char json[] = "[1,2,3]";
|
char json[] = "[1,2,3]";
|
||||||
DeserializationError err = deserializeJson(doc, json);
|
DeserializationError err = deserializeJson(doc, json);
|
||||||
@ -105,7 +118,7 @@ TEST_CASE("copyArray()") {
|
|||||||
REQUIRE(0 == destination[3]);
|
REQUIRE(0 == destination[3]);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("JsonArray -> 1D, without enough space") {
|
SECTION("JsonArray -> int[], without enough space") {
|
||||||
DynamicJsonDocument doc(4096);
|
DynamicJsonDocument doc(4096);
|
||||||
char json[] = "[1,2,3]";
|
char json[] = "[1,2,3]";
|
||||||
DeserializationError err = deserializeJson(doc, json);
|
DeserializationError err = deserializeJson(doc, json);
|
||||||
@ -120,7 +133,24 @@ TEST_CASE("copyArray()") {
|
|||||||
REQUIRE(2 == destination[1]);
|
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<JsonArray>();
|
||||||
|
|
||||||
|
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);
|
DynamicJsonDocument doc(4096);
|
||||||
char json[] = "[1,2,3]";
|
char json[] = "[1,2,3]";
|
||||||
DeserializationError err = deserializeJson(doc, json);
|
DeserializationError err = deserializeJson(doc, json);
|
||||||
@ -136,7 +166,7 @@ TEST_CASE("copyArray()") {
|
|||||||
REQUIRE(0 == destination[3]);
|
REQUIRE(0 == destination[3]);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("JsonArray -> 2D") {
|
SECTION("JsonArray -> int[][]") {
|
||||||
DynamicJsonDocument doc(4096);
|
DynamicJsonDocument doc(4096);
|
||||||
char json[] = "[[1,2],[3],[4]]";
|
char json[] = "[[1,2],[3],[4]]";
|
||||||
|
|
||||||
@ -155,7 +185,7 @@ TEST_CASE("copyArray()") {
|
|||||||
REQUIRE(0 == destination[2][1]);
|
REQUIRE(0 == destination[2][1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("JsonDocument -> 2D") {
|
SECTION("JsonDocument -> int[][]") {
|
||||||
DynamicJsonDocument doc(4096);
|
DynamicJsonDocument doc(4096);
|
||||||
char json[] = "[[1,2],[3],[4]]";
|
char json[] = "[[1,2],[3],[4]]";
|
||||||
|
|
||||||
|
@ -74,7 +74,7 @@ inline size_t copyArray(ArrayConstRef src, T* dst, size_t len) {
|
|||||||
size_t i = 0;
|
size_t i = 0;
|
||||||
for (ArrayConstRef::iterator it = src.begin(); it != src.end() && i < len;
|
for (ArrayConstRef::iterator it = src.begin(); it != src.end() && i < len;
|
||||||
++it)
|
++it)
|
||||||
dst[i++] = *it;
|
dst[i++] = it->as<T>();
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user