@@ -25,6 +25,10 @@ class JsonArray : public detail::VariantOperators<JsonArray> {
2525 // INTERNAL USE ONLY
2626 JsonArray (detail::VariantImpl impl) : impl_(impl) {}
2727
28+ // INTERNAL USE ONLY
29+ JsonArray (detail::VariantData* data, detail::ResourceManager* resources)
30+ : impl_(data, resources) {}
31+
2832 // Returns a JsonVariant pointing to the array.
2933 // https://arduinojson.org/v7/api/jsonvariant/
3034 operator JsonVariant () {
@@ -59,15 +63,15 @@ class JsonArray : public detail::VariantOperators<JsonArray> {
5963 // https://arduinojson.org/v7/api/jsonarray/add/
6064 template <typename T>
6165 bool add (const T& value) const {
62- return impl_. addValue (value);
66+ return doAdd (value);
6367 }
6468
6569 // Appends a value to the array.
6670 // https://arduinojson.org/v7/api/jsonarray/add/
6771 template <typename T,
6872 detail::enable_if_t <!detail::is_const<T>::value, int > = 0 >
6973 bool add (T* value) const {
70- return impl_. addValue (value);
74+ return doAdd (value);
7175 }
7276
7377 // Returns an iterator to the first element of the array.
@@ -203,6 +207,27 @@ class JsonArray : public detail::VariantOperators<JsonArray> {
203207 return impl_;
204208 }
205209
210+ template <typename T>
211+ bool doAdd (const T& value) const {
212+ if (!impl_.isArray ())
213+ return false ;
214+
215+ auto resources = impl_.resources ();
216+ ARDUINOJSON_ASSERT (resources != nullptr );
217+
218+ auto slot = resources->allocVariant ();
219+ if (!slot)
220+ return false ;
221+
222+ if (!JsonVariant (slot.ptr (), resources).set (value)) {
223+ detail::VariantImpl (slot.ptr (), resources).clear ();
224+ resources->freeVariant (slot);
225+ return false ;
226+ }
227+ impl_.addElement (slot);
228+ return true ;
229+ }
230+
206231 mutable detail::VariantImpl impl_;
207232};
208233
0 commit comments