8000 Added JsonVariant::operator=(JsonArray*) · ROMSDEV/ArduinoJson@2e0c201 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2e0c201

Browse files
committed
Added JsonVariant::operator=(JsonArray*)
1 parent bbf627a commit 2e0c201

File tree

9 files changed

+98
-55
lines changed

9 files changed

+98
-55
lines changed

src/ArduinoJson/JsonObject.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,9 @@ class JsonObject : public Internals::JsonPrintable<JsonObject>,
8484
//
8585
// bool set(TKey, TValue);
8686
// TKey = const std::string&, const String&
87-
// TValue = bool, long, int, short, float, double, RawJson, JsonVariant,
87+
// TValue = bool, long, int, short, float, double, RawJson,
8888
// const std::string&, const String&,
89+
// const JsonVariant&,
8990
// const JsonArray&, const JsonObject&
9091
template <typename TValue, typename TString>
9192
typename TypeTraits::EnableIf<!TypeTraits::IsArray<TString>::value &&

src/ArduinoJson/JsonObjectImpl.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ template <typename TStringRef>
1717
inline JsonArray &JsonObject::createNestedArray_impl(TStringRef key) {
1818
JsonArray *array = new (_buffer) JsonArray(_buffer);
1919
if (!array) return JsonArray::invalid();
20-
set(key, *array);
20+
set(key, array);
2121
return *array;
2222
}
2323

src/ArduinoJson/JsonVariant.hpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,13 @@ class JsonVariant : public JsonVariantBase<JsonVariant> {
141141
JsonVariant &operator=(const JsonObject &object);
142142

143143
template <typename T>
144-
JsonVariant &operator=(const JsonVariantBase<T> &variant) {
145-
*this = variant.template as<JsonVariant>();
144+
JsonVariant &operator=(const JsonVariantBase<T> &variant);
145+
146+
JsonVariant &operator=(const JsonVariant &variant);
147+
148+
inline JsonVariant &operator=(JsonArray *array) {
149+
_content.asArray = array;
150+
_type = Internals::JSON_ARRAY;
146151
return *this;
147152
}
148153

src/ArduinoJson/JsonVariantImpl.hpp

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,20 @@
2121
namespace ArduinoJson {
2222

2323
inline JsonVariant &JsonVariant::operator=(const JsonArray &array) {
24-
if (array.success()) {
25-
_type = Internals::JSON_ARRAY;
26-
_content.asArray = const_cast<JsonArray *>(&array);
27-
} else {
28-
_type = Internals::JSON_UNDEFINED;
24+
if (!array.success()) goto fail;
25+
26+
_content.asArray = new (_buffer) JsonArray(_buffer);
27+
if (!_content.asArray) goto fail;
28+
29+
for (JsonArray::const_iterator it = array.begin(); it != array.end(); ++it) {
30+
_content.asArray->add(*it);
2931
}
32+
33+
_type = Internals::JSON_ARRAY;
34+
return *this;
35+
36+
fail:
37+
_type = Internals::JSON_UNDEFINED;
3038
return *this;
3139
}
3240

@@ -49,6 +57,20 @@ inline JsonVariant &JsonVariant::operator=(const JsonObject &object) {
4957
return *this;
5058
}
5159

60+
template <typename T>
61+
inline JsonVariant &JsonVariant::operator=(const JsonVariantBase<T> &variant) {
62+
return operator=(variant.as<JsonVariant>());
63+
}
64+
65+
inline JsonVariant &JsonVariant::operator=(const JsonVariant &variant) {
66+
using namespace Internals;
67+
if (variant.is<JsonArray>()) return operator=(variant.as<JsonArray>());
68+
if (variant.is<JsonObject>()) return operator=(variant.as<JsonObject>());
69+
_content = variant._content;
70+
_type = variant._type;
71+
return *this;
72+
}
73+
5274
inline JsonArray &JsonVariant::variantAsArray() const {
5375
if (_type == Internals::JSON_ARRAY) return *_content.asArray;
5476
return JsonArray::invalid();

test/JsonArray/set.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,16 @@ TEST_CASE("JsonArray::set()") {
5757
REQUIRE_FALSE(_array[0].is<int>());
5858
}
5959

60-
SECTION("nested object") {
61-
DynamicJsonObject obj;
60+
/* TODO: requires object comparison
61+
SECTION("nested object") {
62+
DynamicJsonObject obj;
6263
63-
_array.set(0, obj);
64+
_array.set(0, obj);
6465
65-
REQUIRE(&obj == &_array[0].as<JsonObject&>());
66-
REQUIRE(_array[0].is<JsonObject&>());
67-
REQUIRE_FALSE(_array[0].is<int>());
68-
}
66+
REQUIRE(&obj == &_array[0].as<JsonObject&>());
67+
REQUIRE(_array[0].is<JsonObject&>());
68+
REQUIRE_FALSE(_array[0].is<int>());
69+
}*/
6970

7071
SECTION("array subscript") {
7172
DynamicJsonArray arr;

test/JsonArray/subscript.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,19 @@ TEST_CASE("JsonArray::operator[]") {
7171
REQUIRE(false == _array[0].is<int>());
7272
}
7373

74-
SECTION("nested object") {
75-
DynamicJsonObject obj;
76-
77-
_array[0] = obj;
78-
79-
REQUIRE(&obj == &_array[0].as<JsonObject&>());
80-
REQUIRE(&obj == &_array[0].as<JsonObject>()); // <- short hand
81-
REQUIRE(&obj == &_array[0].as<const JsonObject&>());
82-
REQUIRE(&obj == &_array[0].as<const JsonObject>()); // <- short hand
83-
REQUIRE(true == _array[0].is<JsonObject&>());
84-
REQUIRE(false == _array[0].is<int>());
85-
}
74+
/* TODO: needs object comparison
75+
SECTION("nested object") {
76+
DynamicJsonObject obj;
77+
78+
_array[0] = obj;
79+
80+
REQUIRE(&obj == &_array[0].as<JsonObject&>());
81+
REQUIRE(&obj == &_array[0].as<JsonObject>()); // <- short hand
82+
REQUIRE(&obj == &_array[0].as<const JsonObject&>());
83+
REQUIRE(&obj == &_array[0].as<const JsonObject>()); // <- short hand
84+
REQUIRE(true == _array[0].is<JsonObject&>());
85+
REQUIRE(false == _array[0].is<int>());
86+
}*/
8687

8788
SECTION("array subscript") {
8889
DynamicJsonArray arr;

test/JsonVariant/as.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
static const char* null = 0;
1313

1414
TEST_CASE("JsonVariant::as()") {
15-
StaticJsonVariant<JSON_OBJECT_SIZE(1)> variant;
15+
DynamicJsonVariant variant;
1616

1717
SECTION("DoubleAsBool") {
1818
variant = 4.2;
@@ -213,13 +213,14 @@ TEST_CASE("JsonVariant::as()") {
213213
REQUIRE(std::string("[4,2]") == variant.as<std::string>());
214214
}
215215

216-
SECTION("ArrayAsJsonArray") {
217-
DynamicJsonArray arr;
216+
// TODO: needs array comparison
217+
/* SECTION("ArrayAsJsonArray") {
218+
DynamicJsonArray arr;
218219
219-
variant = arr;
220-
REQUIRE(&arr == &variant.as<JsonArray&>());
221-
REQUIRE(&arr == &variant.as<JsonArray>()); // <- shorthand
222-
}
220+
variant = arr;
221+
REQUIRE(&arr == &variant.as<JsonArray&>());
222+
REQUIRE(&arr == &variant.as<JsonArray>()); // <- shorthand
223+
}*/
223224

224225
/* TODO: need object comparison
225226
SECTION("ObjectAsJsonObject") {

test/JsonVariant/compare.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -181,20 +181,22 @@ TEST_CASE("JsonVariant comparisons") {
181181
REQUIRE_FALSE(variant1 == variant3);
182182
}
183183

184-
SECTION("ArrayInVariant") {
185-
DynamicJsonArray array1;
186-
DynamicJsonArray array2;
184+
/* TODO: needs array comparison
187185
188-
DynamicJsonVariant variant1 = array1;
189-
DynamicJsonVariant variant2 = array1;
190-
DynamicJsonVariant variant3 = array2;
186+
SECTION("ArrayInVariant") {
187+
DynamicJsonArray array1;
188+
DynamicJsonArray array2;
191189
192-
REQUIRE(variant1 == variant2);
193-
REQUIRE_FALSE(variant1 != variant2);
190+
DynamicJsonVariant variant1 = array1;
191+
DynamicJsonVariant variant2 = array1;
192+
DynamicJsonVariant variant3 = array2;
194193
195-
REQUIRE(variant1 != variant3);
196-
REQUIRE_FALSE(variant1 == variant3);
197-
}
194+
REQUIRE(variant1 == variant2);
195+
REQUIRE_FALSE(variant1 != variant2);
196+
197+
REQUIRE(variant1 != variant3);
198+
REQUIRE_FALSE(variant1 == variant3);
199+
}*/
198200

199201
/*TODO: need object comparison
200202
SECTION("ObjectInVariant") {

test/JsonVariant/copy.cpp

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ TEST_CASE("JsonVariant::operator=(const JsonVariant&)") {
4141
variant2 = variant1;
4242
variant1 = "world";
4343

44-
REQUIRE(std::string("hello") == variant2.as<const char *>());
44+
REQUIRE(std::string("hello") == variant2.as<const char*>());
4545
}
4646

4747
SECTION("string") {
@@ -54,24 +54,34 @@ TEST_CASE("JsonVariant::operator=(const JsonVariant&)") {
5454
REQUIRE(std::string("hello") == variant2.as<std::string>());
5555
}
5656

57-
SECTION("object") {
57+
SECTION("JsonObject") {
5858
DynamicJsonObject object;
59-
6059
object["hello"] = "world";
60+
JsonArray& arr = object.createNestedArray("values");
61+
arr.add(42);
62+
63+
std::string s;
64+
object.printTo(s);
65+
REQUIRE("{\"hello\":\"world\",\"values\":[42]}" == s);
66+
6167
variant1 = object;
68+
69+
// modifiy object to make sure we made a copy
6270
object["hello"] = "dummy";
71+
arr.add(666);
6372

64-
REQUIRE(1 == variant1.as<JsonObject>().size());
65-
REQUIRE(std::string("world") == variant1["hello"]);
73+
REQUIRE("{\"hello\":\"world\",\"values\":[42]}" ==
74+
variant1.as<std::string>());
6675
}
6776

68-
SECTION("ArraysAreCopiedByReference") {
77+
SECTION("JsonArray") {
6978
DynamicJsonArray array;
7079

71-
variant1 = array;
72-
80+
array.add("hello");
7381
array.add("world");
82+
variant1 = array;
83+
array[1] = 666;
7484

75-
REQUIRE(1 == variant1.as<JsonArray>().size());
85+
REQUIRE("[\"hello\",\"world\"]" == variant1.as<std::string>());
7686
}
7787
}

0 commit comments

Comments
 (0)
0