File tree Expand file tree Collapse file tree 5 files changed +58
-33
lines changed
Expand file tree Collapse file tree 5 files changed +58
-33
lines changed Original file line number Diff line number Diff line change @@ -76,22 +76,6 @@ inline void VariantImpl::removeElement(size_t index) {
7676 removeElement (at (index));
7777}
7878
79- template <typename T>
80- inline bool VariantImpl::addValue (const T& value) {
81- if (!isArray ())
82- return false ;
83- auto slot = allocVariant ();
84- if (!slot)
85- return false ;
86- JsonVariant variant (slot.ptr (), resources_);
87- if (!variant.set (value)) {
88- freeVariant (slot);
89- return false ;
90- }
91- appendOne (slot);
92- return true ;
93- }
94-
9579inline bool VariantImpl::copyArray (const VariantImpl& src) {
9680 ARDUINOJSON_ASSERT (isNull ());
9781
@@ -102,12 +86,16 @@ inline bool VariantImpl::copyArray(const VariantImpl& src) {
10286
10387 for (auto it = src.createIterator (); !it.done (); it.move ()) {
10488 auto slot = allocVariant ();
105- auto element = VariantImpl (slot.ptr (), resources_);
89+ if (!slot)
90+ return false ;
91+
92+ VariantImpl element (slot.ptr (), resources_);
10693 if (!element.copyVariant (*it)) {
10794 freeVariant (slot);
10895 return false ;
10996 }
110- appendOne (slot);
97+
98+ addElement (slot);
11199 }
112100
113101 return true ;
Original file line number Diff line number Diff line change @@ -90,9 +90,7 @@ class JsonArray : public detail::VariantOperators<JsonArray> {
9090 // https://arduinojson.org/v7/api/jsonarray/set/
9191 bool set (JsonArrayConst src) const {
9292 impl_.clear ();
93- if (src.isNull ())
94- return true ;
95- return impl_.copyCollection (detail::VariantAttorney::getImpl (src));
93+ return impl_.copyArray (detail::VariantAttorney::getImpl (src));
9694 }
9795
9896 // Removes the element at the specified iterator.
Original file line number Diff line number Diff line change @@ -83,10 +83,11 @@ class JsonObject : public detail::VariantOperators<JsonObject> {
8383 // Copies an object.
8484 // https://arduinojson.org/v7/api/jsonobject/set/
8585 bool set (JsonObjectConst src) {
86+ if (isNull () ||
87+ src.isNull ()) // TODO: this check is not consistent with JsonArray
88+ return false ;
8689 impl_.clear ();
87- if (src.isNull ())
88- return true ;
89- return impl_.copyCollection (detail::VariantAttorney::getImpl (src));
90+ return impl_.copyObject (detail::VariantAttorney::getImpl (src));
9091 }
9192
9293 // Gets or sets the member with specified key.
Original file line number Diff line number Diff line change 99
1010ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
1111
12+ inline bool VariantImpl::copyObject (const VariantImpl& src) {
13+ ARDUINOJSON_ASSERT (isNull ());
14+
15+ if (!data_)
16+ return false ;
17+
18+ data_->toObject ();
19+
20+ for (auto it = src.createIterator (); !it.done (); it.move ()) {
21+ auto keySlot = allocVariant ();
22+ if (!keySlot)
23+ return false ;
24+
25+ auto key = VariantImpl (keySlot.ptr (), resources_);
26+ if (!key.copyVariant (*it)) {
27+ freeVariant (keySlot);
28+ return false ;
29+ }
30+
31+ it.move (); // move to value
32+ ARDUINOJSON_ASSERT (!it.done ());
33+
34+ auto valueSlot = allocVariant ();
35+ if (!valueSlot) {
36+ freeVariant (keySlot);
37+ return false ;
38+ }
39+
40+ // TODO: we add the pair before copying the value to be keep the old
41+ // behavior but this is not consistent with issue #2081
42+ appendPair (keySlot, valueSlot);
43+
44+ auto value = VariantImpl (valueSlot.ptr (), resources_);
45+ if (!value.copyVariant (*it))
46+ return false ;
47+ }
48+
49+ return true ;
50+ }
51+
1252template <typename TAdaptedString>
1353inline VariantData* VariantImpl::getMember (TAdaptedString key) const {
1454 auto it = findKey (key);
Original file line number Diff line number Diff line change @@ -354,20 +354,16 @@ class VariantImpl {
354354
355355 void removeMember (iterator it);
356356
357- bool copyArray (const VariantImpl& src) {
358- if (!src.isArray ())
359- return false ;
360- return copyCollection (src);
361- }
362-
363357 bool copyVariant (const VariantImpl& src) {
364358 switch (src.type ()) {
365359 case VariantType::Null:
366360 return true ;
367361
368362 case VariantType::Array:
363+ return copyArray (src);
364+
369365 case VariantType::Object:
370- return copyCollection (src);
366+ return copyObject (src);
371367
372368 case VariantType::RawString:
373369 return setRawString (adaptString (src.asRawString ()));
@@ -379,12 +375,14 @@ class VariantImpl {
379375 return setOwnedString (adaptString (src.asString ()));
380376
381377 default :
382- *data_ = *src.data_ ;
378+ data_->content = src.data_ ->content ;
379+ data_->type = src.data_ ->type ;
383380 return true ;
384381 }
385382 }
386383
387- bool copyCollection (const VariantImpl& src);
384+ bool copyArray (const VariantImpl& src);
385+ bool copyObject (const VariantImpl& src);
388386
389387 bool setBoolean (bool value) {
390388 if (!data_)
You can’t perform that action at this time.
0 commit comments