8000 Converted `JsonArray::copyFrom()/copyTo()` to free functions `copyArr… · java64/ArduinoJson@7ed92be · GitHub
[go: up one dir, main page]

Skip to content

Commit 7ed92be

Browse files
committed
Converted JsonArray::copyFrom()/copyTo() to free functions copyArray()
1 parent c3f71c1 commit 7ed92be

File tree

10 files changed

+197
-188
lines changed

10 files changed

+197
-188
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ HEAD
77
* Decode escaped Unicode characters like \u00DE (issue #304, PR #791)
88
Many thanks to Daniel Schulte (aka @trilader) who implemented this feature.
99
* Add option ARDUINOJSON_DECODE_UNICODE to enable it
10+
* Converted `JsonArray::copyFrom()/copyTo()` to free functions `copyArray()`
11+
* Renamed `JsonArray::copyFrom()` and `JsonObject::copyFrom()` to `set()`
1012

1113
v6.8.0-beta (2019-01-30)
1214
-----------

src/ArduinoJson.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "ArduinoJson/Array/ArrayImpl.hpp"
1717
#include "ArduinoJson/Array/ElementProxy.hpp"
18+
#include "ArduinoJson/Array/Utilities.hpp"
1819
#include "ArduinoJson/Collection/CollectionImpl.hpp"
1920
#include "ArduinoJson/Object/MemberProxy.hpp"
2021
#include "ArduinoJson/Object/ObjectImpl.hpp"
@@ -39,6 +40,7 @@ typedef ARDUINOJSON_NAMESPACE::String JsonString;
3940
typedef ARDUINOJSON_NAMESPACE::UInt JsonUInt;
4041
typedef ARDUINOJSON_NAMESPACE::VariantConstRef JsonVariantConst;
4142
typedef ARDUINOJSON_NAMESPACE::VariantRef JsonVariant;
43+
using ARDUINOJSON_NAMESPACE::copyArray;
4244
using ARDUINOJSON_NAMESPACE::DeserializationError;
4345
using ARDUINOJSON_NAMESPACE::deserializeJson;
4446
using ARDUINOJSON_NAMESPACE::deserializeMsgPack;

src/ArduinoJson/Array/ArrayRef.hpp

Lines changed: 1 addition & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -115,65 +115,12 @@ class ArrayRef : public ArrayRefBase<CollectionData>,
115115
return iterator();
116116
}
117117

118-
// Imports a 1D array
119-
template <typename T, size_t N>
120-
FORCE_INLINE bool copyFrom(T (&array)[N]) const {
121-
return copyFrom(array, N);
122-
}
123-
124-
// Imports a 1D array
125-
template <typename T>
126-
bool copyFrom(T* array, size_t len) const {
127-
bool ok = true;
128-
for (size_t i = 0; i < len; i++) {
129-
ok &= add(array[i]);
130-
}
131-
return ok;
132-
}
133-
134-
// Imports a 2D array
135-
template <typename T, size_t N1, size_t N2>
136-
bool copyFrom(T (&array)[N1][N2]) const {
137-
bool ok = true;
138-
for (size_t i = 0; i < N1; i++) {
139-
ArrayRef nestedArray = createNestedArray();
140-
for (size_t j = 0; j < N2; j++) {
141-
ok &= nestedArray.add(array[i][j]);
142-
}
143-
}
144-
return ok;
145-
}
146-
147118
// Copy a ArrayRef
148-
FORCE_INLINE bool copyFrom(ArrayConstRef src) const {
119+
FORCE_INLINE bool set(ArrayConstRef src) const {
149120
if (!_data || !src._data) return false;
150121
return _data->copyFrom(*src._data, _pool);
151122
}
152123

153-
// Exports a 1D array
154-
template <typename T, size_t N>
155-
FORCE_INLINE size_t copyTo(T (&array)[N]) const {
156-
return copyTo(array, N);
157-
}
158-
159-
// Exports a 1D array
160-
template <typename T>
161-
size_t copyTo(T* array, size_t len) const {
162-
size_t i = 0;
163-
for (iterator it = begin(); it != end() && i < len; ++it) array[i++] = *it;
164-
return i;
165-
}
166-
167-
// Exports a 2D array
168-
template <typename T, size_t N1, size_t N2>
169-
void copyTo(T (&array)[N1][N2]) const {
170-
if (!_data) return;
171-
size_t i = 0;
172-
for (iterator it = begin(); it != end() && i < N1; ++it) {
173-
it->as<ArrayRef>().copyTo(array[i++]);
174-
}
175-
}
176-
177124
FORCE_INLINE bool operator==(ArrayRef rhs) const {
178125
return arrayEquals(_data, rhs._data);
179126
}

src/ArduinoJson/Array/Utilities.hpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// ArduinoJson - arduinojson.org
2+
// Copyright Benoit Blanchon 2014-2019
3+
// MIT License
4+
5+
#pragma once
6+
7+
#include "ArrayRef.hpp"
8+
9+
namespace ARDUINOJSON_NAMESPACE {
10+
11+
// Copy a 1D array to a JsonArray
12+
template <typename T, size_t N>
13+
inline bool copyArray(T (&src)[N], ArrayRef dst) {
14+
return copyArray(src, N, dst);
15+
}
16+
17+
// Copy a 1D array to a JsonArray
18+
template <typename T>
19+
inline bool copyArray(T* src, size_t len, ArrayRef dst) {
20+
bool ok = true;
21+
for (size_t i = 0; i < len; i++) {
22+
ok &= dst.add(src[i]);
23+
}
24+
return ok;
25+
}
26+
27+
// Copy a 2D array to a JsonArray
28+
template <typename T, size_t N1, size_t N2>
29+
inline bool copyArray(T (&src)[N1][N2], ArrayRef dst) {
30+
bool ok = true;
31+
for (size_t i = 0; i < N1; i++) {
32+
ArrayRef nestedArray = dst.createNestedArray();
33+
for (size_t j = 0; j < N2; j++) {
34+
ok &= nestedArray.add(src[i][j]);
35+
}
36+
}
37+
return ok;
38+
}
39+
40+
// Copy a JsonArray to a 1D array
41+
template <typename T, size_t N>
42+
inline size_t copyArray(ArrayConstRef src, T (&dst)[N]) {
43+
return copyArray(src, dst, N);
44+
}
45+
46+
// Copy a JsonArray to a 1D array
47+
template <typename T>
48+
inline size_t copyArray(ArrayConstRef src, T* dst, size_t len) {
49+
size_t i = 0;
50+
for (ArrayConstRef::iterator it = src.begin(); it != src.end() && i < len;
51+
++it)
52+
dst[i++] = *it;
53+
return i;
54+
}
55+
56+
// Copy a JsonArray to a 2D array
57+
template <typename T, size_t N1, size_t N2>
58+
inline void copyArray(ArrayConstRef src, T (&dst)[N1][N2]) {
59+
size_t i = 0;
60+
for (ArrayConstRef::iterator it = src.begin(); it != src.end() && i < N1;
61+
++it) {
62+
copyArray(it->as<ArrayConstRef>(), dst[i++]);
63+
}
64+
}
65+
66+
} // namespace ARDUINOJSON_NAMESPACE

src/ArduinoJson/Object/ObjectRef.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ class ObjectRef : public ObjectRefBase<CollectionData>,
161161
_data->clear();
162162
}
163163

164-
FORCE_INLINE bool copyFrom(ObjectConstRef src) {
164+
FORCE_INLINE bool set(ObjectConstRef src) {
165165
if (!_data || !src._data) return false;
166166
return _data->copyFrom(*src._data, _pool);
167167
}

test/JsonArray/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44

55
add_executable(JsonArrayTests
66
add.cpp
7-
copyFrom.cpp
8-
copyTo.cpp
7+
copyArray.cpp
98
createNested.cpp
109
equals.cpp
1110
get.cpp

test/JsonArray/copyArray.cpp

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
// ArduinoJson - arduinojson.org
2+
// Copyright Benoit Blanchon 2014-2019
3+
// MIT License
4+
5+
#include <ArduinoJson.h>
6+
#include <catch.hpp>
7+
8+
TEST_CASE("copyArray()") {
9+
SECTION("1D -> JsonArray") {
10+
DynamicJsonDocument doc(4096);
11+
JsonArray array = doc.to<JsonArray>();
12+
char json[32];
13+
int source[] = {1, 2, 3};
14+
15+
bool ok = copyArray(source, array);
16+
REQUIRE(ok);
17+
18+
serializeJson(array, json, sizeof(json));
19+
REQUIRE(std::string("[1,2,3]") == json);
20+
}
21+
22+
SECTION("1D -> JsonArray, but not enough memory") {
23+
const size_t SIZE = JSON_ARRAY_SIZE(2);
24+
StaticJsonDocument<SIZE> doc;
25+
JsonArray array = doc.to<JsonArray>();
26+
char json[32];
27+
int source[] = {1, 2, 3};
28+
29+
bool ok = copyArray(source, array);
30+
REQUIRE_FALSE(ok);
31+
32+
serializeJson(array, json, sizeof(json));
33+
REQUIRE(std::string("[1,2]") == json);
34+
}
35+
36+
SECTION("2D -> JsonArray") {
37+
DynamicJsonDocument doc(4096);
38+
JsonArray array = doc.to<JsonArray>();
39+
char json[32];
40+
int source[][3] = {{1, 2, 3}, {4, 5, 6}};
41+
42+
bool ok = copyArray(source, array);
43+
REQUIRE(ok);
44+
45+
serializeJson(array, json, sizeof(json));
46+
REQUIRE(std::string("[[1,2,3],[4,5,6]]") == json);
47+
}
48+
49+
SECTION("2D -> JsonArray, but not enough memory") {
50+
const size_t SIZE =
51+
JSON_ARRAY_SIZE(2) + JSON_ARRAY_SIZE(3) + JSON_ARRAY_SIZE(2);
52+
StaticJsonDocument<SIZE> doc;
53+
JsonArray array = doc.to<JsonArray>();
54+
char json[32] = "";
55+
int source[][3] = {{1, 2, 3}, {4, 5, 6}};
56+
57+
CAPTURE(SIZE)
58+
59+
bool ok = copyArray(source, array);
60+
CAPTURE(doc.memoryUsage());
61+
CHECK_FALSE(ok);
62+
63+
serializeJson(array, json, sizeof(json));
64+
REQUIRE(std::string("[[1,2,3],[4,5]]") == json);
65+
}
66+
67+
SECTION("JsonArray -> 1D, with more space than needed") {
68+
DynamicJsonDocument doc(4096);
69+
char json[] = "[1,2,3]";
70+
DeserializationError err = deserializeJson(doc, json);
71+
REQUIRE(err == DeserializationError::Ok);
72+
JsonArray array = doc.as<JsonArray>();
73+
74+
int destination[4] = {0};
75+
size_t result = copyArray(array, destination);
76+
77+
REQUIRE(3 == result);
78+
REQUIRE(1 == destination[0]);
79+
REQUIRE(2 == destination[1]);
80+
REQUIRE(3 == destination[2]);
81+
REQUIRE(0 == destination[3]);
82+
}
83+
84+
SECTION("JsonArray -> 1D, without enough space") {
85+
DynamicJsonDocument doc(4096);
86+
char json[] = "[1,2,3]";
87+
DeserializationError err = deserializeJson(doc, json);
88+
REQUIRE(err == DeserializationError::Ok);
89+
JsonArray array = doc.as<JsonArray>();
90+
91+
int destination[2] = {0};
92+
size_t result = copyArray(array, destination);
93+
94+
REQUIRE(2 == result);
95+
REQUIRE(1 == destination[0]);
96+
REQUIRE(2 == destination[1]);
97+
}
98+
99+
SECTION("JsonArray -> 2D") {
100+
DynamicJsonDocument doc(4096);
101+
char json[] = "[[1,2],[3],[4]]";
102+
103+
DeserializationError err = deserializeJson(doc, json);
104+
REQUIRE(err == DeserializationError::Ok);
105+
JsonArray array = doc.as<JsonArray>();
106+
107+
int destination[3][2] = {{0}};
108+
copyArray(array, destination);
109+
110+
REQUIRE(1 == destination[0][0]);
111+
REQUIRE(2 == destination[0][1]);
112+
REQUIRE(3 == destination[1][0]);
113+
REQUIRE(0 == destination[1][1]);
114+
REQUIRE(4 == destination[2][0]);
115+
REQUIRE(0 == destination[2][1]);
116+
}
117+
}

test/JsonArray/copyFrom.cpp

Lines changed: 0 additions & 66 deletions
This file was deleted.

0 commit comments

Comments
 (0)
0