diff --git a/CHANGELOG.md b/CHANGELOG.md index ff6c5ea9..e0f44148 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ HEAD * Added `JsonVariant::as()` as a synonym for `JsonVariant::as()` (issue #257) * Added example `JsonHttpClient` (issue #256) +* Added `JsonArray::copyTo()` and `JsonArray::copyFrom()` (issue #254) v5.1.1 ------ diff --git a/include/ArduinoJson/JsonArray.hpp b/include/ArduinoJson/JsonArray.hpp index 62c3ec6c..88e5f880 100644 --- a/include/ArduinoJson/JsonArray.hpp +++ b/include/ArduinoJson/JsonArray.hpp @@ -161,6 +161,59 @@ class JsonArray : public Internals::JsonPrintable, // Serialize the array to the specified JsonWriter. void writeTo(Internals::JsonWriter &writer) const; + // Imports a 1D array + template + bool copyFrom(T(&array)[N]) { + return copyFrom(array, N); + } + + // Imports a 1D array + template + bool copyFrom(T *array, size_t len) { + bool ok = true; + for (size_t i = 0; i < len; i++) { + ok &= add(array[i]); + } + return ok; + } + + // Imports a 2D array + template + bool copyFrom(T(&array)[N1][N2]) { + bool ok = true; + for (size_t i = 0; i < N1; i++) { + JsonArray &nestedArray = createNestedArray(); + for (size_t j = 0; j < N2; j++) { + ok &= nestedArray.add(array[i][j]); + } + } + return ok; + } + + // Exports a 1D array + template + size_t copyTo(T(&array)[N]) const { + return copyTo(array, N); + } + + // Exports a 1D array + template + size_t copyTo(T *array, size_t len) const { + size_t i = 0; + for (const_iterator it = begin(); it != end() && i < len; ++it) + array[i++] = *it; + return i; + } + + // Exports a 2D array + template + void copyTo(T(&array)[N1][N2]) const { + size_t i = 0; + for (const_iterator it = begin(); it != end() && i < N1; ++it) { + it->asArray().copyTo(array[i++]); + } + } + private: node_type *getNodeAt(size_t index) const; diff --git a/test/JsonArray_CopyFrom_Tests.cpp b/test/JsonArray_CopyFrom_Tests.cpp new file mode 100644 index 00000000..02dbd505 --- /dev/null +++ b/test/JsonArray_CopyFrom_Tests.cpp @@ -0,0 +1,64 @@ +// Copyright Benoit Blanchon 2014-2016 +// MIT License +// +// Arduino JSON library +// https://github.com/bblanchon/ArduinoJson +// If you like this project, please add a star! + +#include +#include + +TEST(JsonArray_CopyFrom_Tests, OneDimension) { + DynamicJsonBuffer jsonBuffer; + JsonArray& array = jsonBuffer.createArray(); + char json[32]; + int source[] = {1, 2, 3}; + + bool ok = array.copyFrom(source); + ASSERT_TRUE(ok); + + array.printTo(json, sizeof(json)); + ASSERT_STREQ("[1,2,3]", json); +} + +TEST(JsonArray_CopyFrom_Tests, OneDimension_JsonBufferTooSmall) { + const size_t SIZE = JSON_ARRAY_SIZE(2); + StaticJsonBuffer jsonBuffer; + JsonArray& array = jsonBuffer.createArray(); + char json[32]; + int source[] = {1, 2, 3}; + + bool ok = array.copyFrom(source); + ASSERT_FALSE(ok); + + array.printTo(json, sizeof(json)); + ASSERT_STREQ("[1,2]", json); +} + +TEST(JsonArray_CopyFrom_Tests, TwoDimensions) { + DynamicJsonBuffer jsonBuffer; + JsonArray& array = jsonBuffer.createArray(); + char json[32]; + int source[][3] = {{1, 2, 3}, {4, 5, 6}}; + + bool ok = array.copyFrom(source); + ASSERT_TRUE(ok); + + array.printTo(json, sizeof(json)); + ASSERT_STREQ("[[1,2,3],[4,5,6]]", json); +} + +TEST(JsonArray_CopyFrom_Tests, TwoDimensions_JsonBufferTooSmall) { + const size_t SIZE = + JSON_ARRAY_SIZE(2) + JSON_ARRAY_SIZE(3) + JSON_ARRAY_SIZE(2); + StaticJsonBuffer jsonBuffer; + JsonArray& array = jsonBuffer.createArray(); + char json[32]; + int source[][3] = {{1, 2, 3}, {4, 5, 6}}; + + bool ok = array.copyFrom(source); + ASSERT_FALSE(ok); + + array.printTo(json, sizeof(json)); + ASSERT_STREQ("[[1,2,3],[4,5]]", json); +} diff --git a/test/JsonArray_CopyTo_Tests.cpp b/test/JsonArray_CopyTo_Tests.cpp new file mode 100644 index 00000000..41488ef4 --- /dev/null +++ b/test/JsonArray_CopyTo_Tests.cpp @@ -0,0 +1,56 @@ +// Copyright Benoit Blanchon 2014-2016 +// MIT License +// +// Arduino JSON library +// https://github.com/bblanchon/ArduinoJson +// If you like this project, please add a star! + +#include +#include + +TEST(JsonArray_CopyTo_Tests, BiggerOneDimensionIntegerArray) { + char json[] = "[1,2,3]"; + + DynamicJsonBuffer jsonBuffer; + JsonArray& array = jsonBuffer.parseArray(json); + + int destination[4] = {0}; + size_t result = array.copyTo(destination); + + ASSERT_EQ(3, result); + ASSERT_EQ(1, destination[0]); + ASSERT_EQ(2, destination[1]); + ASSERT_EQ(3, destination[2]); + ASSERT_EQ(0, destination[3]); +} + +TEST(JsonArray_CopyTo_Tests, SmallerOneDimensionIntegerArray) { + char json[] = "[1,2,3]"; + + DynamicJsonBuffer jsonBuffer; + JsonArray& array = jsonBuffer.parseArray(json); + + int destination[2] = {0}; + size_t result = array.copyTo(destination); + + ASSERT_EQ(2, result); + ASSERT_EQ(1, destination[0]); + ASSERT_EQ(2, destination[1]); +} + +TEST(JsonArray_CopyTo_Tests, TwoOneDimensionIntegerArray) { + char json[] = "[[1,2],[3],[4]]"; + + DynamicJsonBuffer jsonBuffer; + JsonArray& array = jsonBuffer.parseArray(json); + + int destination[3][2] = {0}; + array.copyTo(destination); + + ASSERT_EQ(1, destination[0][0]); + ASSERT_EQ(2, destination[0][1]); + ASSERT_EQ(3, destination[1][0]); + ASSERT_EQ(0, destination[1][1]); + ASSERT_EQ(4, destination[2][0]); + ASSERT_EQ(0, destination[2][1]); +}