@@ -46,10 +46,11 @@ class VariantImpl {
4646#endif
4747
4848 case VariantType::Array:
49- return visit.visit (asArray ( ));
49+ return visit.visit (ArrayImpl (&data_-> content . asCollection , resources_ ));
5050
5151 case VariantType::Object:
52- return visit.visit (asObject ());
52+ return visit.visit (
53+ ObjectImpl (&data_->content .asCollection , resources_));
5354
5455 case VariantType::TinyString:
5556 return visit.visit (JsonString (data_->content .asTinyString ));
@@ -88,14 +89,16 @@ class VariantImpl {
8889 }
8990
9091 VariantData* addElement () {
91- auto array = isNull () ? toArray () : asArray ();
92- return array.addElement ();
92+ if (!data_)
93+ return nullptr ;
94+ return ArrayImpl (data_->getOrCreateArray (), resources_).addElement ();
9395 }
9496
9597 template <typename T>
9698 bool addValue (const T& value) {
97- auto array = isNull () ? toArray () : asArray ();
98- return array.addValue (value);
99+ if (!data_)
100+ return false ;
101+ return ArrayImpl (data_->getOrCreateArray (), resources_).addValue (value);
99102 }
100103
101104 bool asBoolean () const {
@@ -129,16 +132,6 @@ class VariantImpl {
129132 }
130133 }
131134
132- ArrayImpl asArray () {
133- return ArrayImpl (isArray () ? &data_->content .asCollection : nullptr ,
134- resources_);
135- }
136-
137- CollectionImpl asCollection () {
138- return CollectionImpl (
139- isCollection () ? &data_->content .asCollection : nullptr , resources_);
140- }
141-
142135 template <typename T>
143136 T asFloat () const {
144137 if (!data_)
@@ -231,11 +224,6 @@ class VariantImpl {
231224 return parseNumber<T>(str);
232225 }
233226
234- ObjectImpl asObject () {
235- return ObjectImpl (isObject () ? &data_->content .asCollection : nullptr ,
236- resources_);
237- }
238-
239227 JsonString asRawString () const {
240228 switch (type ()) {
241229 case VariantType::RawString:
@@ -274,25 +262,33 @@ class VariantImpl {
274262#endif
275263
276264 VariantData* getElement (size_t index) {
277- return asArray ().getElement (index);
265+ if (!data_)
266+ return nullptr ;
267+ return ArrayImpl (data_->asArray (), resources_).getElement (index);
278268 }
279269
280270 template <typename TAdaptedString>
281271 VariantData* getMember (TAdaptedString key) {
282- return asObject ().getMember (key);
272+ if (!data_)
273+ return nullptr ;
274+ return ObjectImpl (data_->asObject (), resources_).getMember (key);
283275 }
284276
285277 VariantData* getOrAddElement (size_t index) {
286- auto array = isNull () ? toArray () : asArray ();
287- return array.getOrAddElement (index);
278+ if (!data_)
279+ return nullptr ;
280+ return ArrayImpl (data_->getOrCreateArray (), resources_)
281+ .getOrAddElement (index);
288282 }
289283
290284 template <typename TAdaptedString>
291285 VariantData* getOrAddMember (TAdaptedString key) {
292286 if (key.isNull ())
293287 return nullptr ;
294- auto obj = isNull () ? toObject () : asObject ();
295- return obj.getOrAddMember (key);
288+ if (!data_)
289+ return nullptr ;
290+ return ObjectImpl (data_->getOrCreateObject (), resources_)
291+ .getOrAddMember (key);
296292 }
297293
298294 bool isArray () const {
@@ -352,16 +348,22 @@ class VariantImpl {
352348 }
353349
354350 size_t nesting () {
355- return asCollection ().nesting ();
351+ if (!data_)
352+ return 0 ;
353+ return CollectionImpl (data_->asCollection (), resources_).nesting ();
356354 }
357355
358356 void removeElement (size_t index) {
359- asArray ().removeElement (index);
357+ if (!data_)
358+ return ;
359+ ArrayImpl (data_->asArray (), resources_).removeElement (index);
360360 }
361361
362362 template <typename TAdaptedString>
363363 void removeMember (TAdaptedString key) {
364- asObject ().removeMember (key);
364+ if (!data_)
365+ return ;
366+ ObjectImpl (data_->asObject (), resources_).removeMember (key);
365367 }
366368
367369 bool setBoolean (bool value) {
@@ -466,31 +468,15 @@ class VariantImpl {
466468 bool setLinkedString (const char * s);
467469
468470 size_t size () {
469- if (isObject ())
470- return asObject ().size ();
471-
472- if (isArray ())
473- return asArray ().size ();
471+ if (!data_)
472+ return 0 ;
474473
475- return 0 ;
476- }
474+ auto size = CollectionImpl (data_->asCollection (), resources_).size ();
477475
478- ArrayImpl toArray () {
479- ARDUINOJSON_ASSERT (type () == VariantType::Null); // must call clear() first
480- if (!data_)
481- return ArrayImpl ();
482- data_->type = VariantType::Array;
483- return ArrayImpl (new (&data_->content .asCollection ) CollectionData (),
484- resources_);
485- }
476+ if (data_->type == VariantType::Object)
477+ size /= 2 ;
486478
487- ObjectImpl toObject () {
488- ARDUINOJSON_ASSERT (type () == VariantType::Null); // must call clear() first
489- if (!data_)
490- return ObjectImpl ();
491- data_->type = VariantType::Object;
492- return ObjectImpl (new (&data_->content .asCollection ) CollectionData (),
493- resources_);
479+ return size;
494480 }
495481
496482 VariantType type () const {
@@ -566,7 +552,7 @@ inline void VariantImpl::clear() {
566552 resources_->freeEightByte (data_->content .asSlotId );
567553#endif
568554
569- asCollection ().clear ();
555+ CollectionImpl (data_-> asCollection (), resources_ ).clear ();
570556
571557 data_->type = VariantType::Null;
572558}
0 commit comments