• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

bblanchon / ArduinoJson / 18653349747

20 Oct 2025 12:56PM UTC coverage: 99.34% (-0.08%) from 99.417%
18653349747

push

github

bblanchon
Return `bool` from all built-in `toJson` converters

104 of 108 new or added lines in 8 files covered. (96.3%)

3 existing lines in 2 files now uncovered.

3916 of 3942 relevant lines covered (99.34%)

10349.1 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.75
/src/ArduinoJson/Array/JsonArray.hpp
1
// ArduinoJson - https://arduinojson.org
2
// Copyright © 2014-2025, Benoit BLANCHON
3
// MIT License
4

5
#pragma once
6

7
#include <ArduinoJson/Array/ElementProxy.hpp>
8
#include <ArduinoJson/Array/JsonArrayConst.hpp>
9

10
ARDUINOJSON_BEGIN_PUBLIC_NAMESPACE
11

12
class JsonObject;
13

14
// A reference to an array in a JsonDocument
15
// https://arduinojson.org/v7/api/jsonarray/
16
class JsonArray : public detail::VariantOperators<JsonArray> {
17
  friend class detail::VariantAttorney;
18

19
 public:
20
  using iterator = JsonArrayIterator;
21

22
  // Constructs an unbound reference.
23
  JsonArray() {}
18✔
24

25
  // INTERNAL USE ONLY
26
  JsonArray(detail::VariantData* data, detail::ResourceManager* resources)
557✔
27
      : impl_(data, resources) {}
557✔
28

29
  // INTERNAL USE ONLY
30
  JsonArray(const detail::VariantImpl& impl) : impl_(impl) {}
78✔
31

32
  // Returns a JsonVariant pointing to the array.
33
  // https://arduinojson.org/v7/api/jsonvariant/
34
  operator JsonVariant() {
8✔
35
    return JsonVariant(getData(), getResourceManager());
8✔
36
  }
37

38
  // Returns a read-only reference to the array.
39
  // https://arduinojson.org/v7/api/jsonarrayconst/
40
  operator JsonArrayConst() const {
44✔
41
    return JsonArrayConst(getData(), getResourceManager());
44✔
42
  }
43

44
  // Appends a new (empty) element to the array.
45
  // Returns a reference to the new element.
46
  // https://arduinojson.org/v7/api/jsonarray/add/
47
  template <typename T, detail::enable_if_t<
48
                            !detail::is_same<T, JsonVariant>::value, int> = 0>
49
  T add() const {
36✔
50
    return add<JsonVariant>().to<T>();
36✔
51
  }
52

53
  // Appends a new (null) element to the array.
54
  // Returns a reference to the new element.
55
  // https://arduinojson.org/v7/api/jsonarray/add/
56
  template <typename T, detail::enable_if_t<
57
                            detail::is_same<T, JsonVariant>::value, int> = 0>
58
  JsonVariant add() const {
230✔
59
    return JsonVariant(impl_.addNewElement(), impl_.resources());
230✔
60
  }
61

62
  // Appends a value to the array.
63
  // https://arduinojson.org/v7/api/jsonarray/add/
64
  template <typename T>
65
  bool add(const T& value) const {
66,674✔
66
    if (!impl_.isArray())
66,674✔
67
      return false;
9✔
68
    return addValue(value, impl_.data(), impl_.resources());
66,665✔
69
  }
70

71
  // Appends a value to the array.
72
  // https://arduinojson.org/v7/api/jsonarray/add/
73
  template <typename T,
74
            detail::enable_if_t<!detail::is_const<T>::value, int> = 0>
75
  bool add(T* value) const {
12✔
76
    if (!impl_.isArray())
12✔
UNCOV
77
      return false;
×
78
    return addValue(value, impl_.data(), impl_.resources());
12✔
79
  }
80

81
  // Returns an iterator to the first element of the array.
82
  // https://arduinojson.org/v7/api/jsonarray/begin/
83
  iterator begin() const {
7✔
84
    return iterator(impl_.createIterator(), impl_.resources());
7✔
85
  }
86

87
  // Returns an iterator following the last element of the array.
88
  // https://arduinojson.org/v7/api/jsonarray/end/
89
  iterator end() const {
7✔
90
    return iterator();
7✔
91
  }
92

93
  // Copies an array.
94
  // https://arduinojson.org/v7/api/jsonarray/set/
95
  bool set(JsonArrayConst src) const {
13✔
96
    clear();
13✔
97
    for (auto element : src) {
19✔
98
      if (!add(element))
7✔
99
        return false;
1✔
100
    }
101

102
    return true;
12✔
103
  }
104

105
  // Removes the element at the specified iterator.
106
  // https://arduinojson.org/v7/api/jsonarray/remove/
107
  void remove(iterator it) const {
6✔
108
    impl_.removeElement(it.iterator_);
6✔
109
  }
6✔
110

111
  // Removes the element at the specified index.
112
  // https://arduinojson.org/v7/api/jsonarray/remove/
113
  void remove(size_t index) const {
6✔
114
    impl_.removeElement(index);
6✔
115
  }
6✔
116

117
  // Removes the element at the specified index.
118
  // https://arduinojson.org/v7/api/jsonarray/remove/
119
  template <typename TVariant,
120
            detail::enable_if_t<detail::IsVariant<TVariant>::value, int> = 0>
121
  void remove(const TVariant& variant) const {
2✔
122
    if (variant.template is<size_t>())
2✔
123
      remove(variant.template as<size_t>());
1✔
124
  }
2✔
125

126
  // Removes all the elements of the array.
127
  // https://arduinojson.org/v7/api/jsonarray/clear/
128
  void clear() const {
16✔
129
    impl_.empty();
16✔
130
  }
16✔
131

132
  // Gets or sets the element at the specified index.
133
  // https://arduinojson.org/v7/api/jsonarray/subscript/
134
  template <typename T,
135
            detail::enable_if_t<detail::is_integral<T>::value, int> = 0>
136
  detail::ElementProxy<JsonArray> operator[](T index) const {
263✔
137
    return {*this, size_t(index)};
263✔
138
  }
139

140
  // Gets or sets the element at the specified index.
141
  // https://arduinojson.org/v7/api/jsonarray/subscript/
142
  template <typename TVariant,
143
            detail::enable_if_t<detail::IsVariant<TVariant>::value, int> = 0>
144
  detail::ElementProxy<JsonArray> operator[](const TVariant& variant) const {
2✔
145
    if (variant.template is<size_t>())
2✔
146
      return {*this, variant.template as<size_t>()};
1✔
147
    else
148
      return {*this, size_t(-1)};
1✔
149
  }
150

151
  operator JsonVariantConst() const {
271✔
152
    return JsonVariantConst(getData(), getResourceManager());
271✔
153
  }
154

155
  // Returns true if the reference is unbound.
156
  // https://arduinojson.org/v7/api/jsonarray/isnull/
157
  bool isNull() const {
21✔
158
    return impl_.isNull();
21✔
159
  }
160

161
  // Returns true if the reference is bound.
162
  // https://arduinojson.org/v7/api/jsonarray/isnull/
163
  operator bool() const {
2✔
164
    return !isNull();
2✔
165
  }
166

167
  // Returns the depth (nesting level) of the array.
168
  // https://arduinojson.org/v7/api/jsonarray/nesting/
169
  size_t nesting() const {
5✔
170
    return impl_.nesting();
5✔
171
  }
172

173
  // Returns the number of elements in the array.
174
  // https://arduinojson.org/v7/api/jsonarray/size/
175
  size_t size() const {
79✔
176
    return impl_.size();
79✔
177
  }
178

179
  // DEPRECATED: use add<JsonVariant>() instead
180
  ARDUINOJSON_DEPRECATED("use add<JsonVariant>() instead")
181
  JsonVariant add() const {
1✔
182
    return add<JsonVariant>();
1✔
183
  }
184

185
  // DEPRECATED: use add<JsonArray>() instead
186
  ARDUINOJSON_DEPRECATED("use add<JsonArray>() instead")
187
  JsonArray createNestedArray() const {
1✔
188
    return add<JsonArray>();
1✔
189
  }
190

191
  // DEPRECATED: use add<JsonObject>() instead
192
  ARDUINOJSON_DEPRECATED("use add<JsonObject>() instead")
193
  JsonObject createNestedObject() const;
194

195
  // DEPRECATED: always returns zero
196
  ARDUINOJSON_DEPRECATED("always returns zero")
197
  size_t memoryUsage() const {
1✔
198
    return 0;
1✔
199
  }
200

201
 private:
202
  detail::ResourceManager* getResourceManager() const {
857✔
203
    return impl_.resources();
857✔
204
  }
205

206
  detail::VariantData* getData() const {
568✔
207
    return impl_.data();
568✔
208
  }
209

210
  detail::VariantData* getOrCreateData() const {
22✔
211
    return impl_.data();
22✔
212
  }
213

214
  // HACK: this function has been pulled out of VariantImpl to avoid the
215
  // circular dependency between VariantImpl and JsonVariant
216
  template <typename T>
217
  static bool addValue(const T& value, detail::VariantData* data,
66,677✔
218
                       detail::ResourceManager* resources) {
219
    ARDUINOJSON_ASSERT(data != nullptr);
220
    ARDUINOJSON_ASSERT(data->isArray());
221
    ARDUINOJSON_ASSERT(resources != nullptr);
222

223
    auto slot = resources->allocVariant();
66,677✔
224
    if (!slot)
66,677✔
225
      return false;
7✔
226

227
    if (!JsonVariant(slot.ptr(), resources).set(value)) {
66,670✔
228
      detail::VariantImpl::freeVariant(slot, resources);
4✔
229
      return false;
4✔
230
    }
231

232
    detail::VariantImpl::addElement(slot, data, resources);
66,666✔
233
    return true;
66,666✔
234
  }
235

236
  mutable detail::VariantImpl impl_;
237
};
238

239
ARDUINOJSON_END_PUBLIC_NAMESPACE
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc