Added JsonArray::copyTo() and JsonArray::copyFrom() (issue #254)

This commit is contained in:
Benoit Blanchon
2016-04-14 20:06:38 +02:00
parent 623aeee9bf
commit c77c3f33ef
4 changed files with 174 additions and 0 deletions

View File

@ -6,6 +6,7 @@ HEAD
* Added `JsonVariant::as<char*>()` as a synonym for `JsonVariant::as<const char*>()` (issue #257) * Added `JsonVariant::as<char*>()` as a synonym for `JsonVariant::as<const char*>()` (issue #257)
* Added example `JsonHttpClient` (issue #256) * Added example `JsonHttpClient` (issue #256)
* Added `JsonArray::copyTo()` and `JsonArray::copyFrom()` (issue #254)
v5.1.1 v5.1.1
------ ------

View File

@ -161,6 +161,59 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
// Serialize the array to the specified JsonWriter. // Serialize the array to the specified JsonWriter.
void writeTo(Internals::JsonWriter &writer) const; void writeTo(Internals::JsonWriter &writer) const;
// Imports a 1D array
template <typename T, size_t N>
bool copyFrom(T(&array)[N]) {
return copyFrom(array, N);
}
// Imports a 1D array
template <typename T>
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 <typename T, size_t N1, size_t N2>
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 <typename T, size_t N>
size_t copyTo(T(&array)[N]) const {
return copyTo(array, N);
}
// Exports a 1D array
template <typename T>
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 <typename T, size_t N1, size_t N2>
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: private:
node_type *getNodeAt(size_t index) const; node_type *getNodeAt(size_t index) const;

View File

@ -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 <ArduinoJson.h>
#include <gtest/gtest.h>
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<SIZE> 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<SIZE> 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);
}

View File

@ -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 <ArduinoJson.h>
#include <gtest/gtest.h>
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]);
}