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

bblanchon / ArduinoJson / 13563980954

27 Feb 2025 10:13AM CUT coverage: 99.325%. First build
13563980954

push

github

bblanchon
Reduce code size

13 of 13 new or added lines in 1 file covered. (100.0%)

3974 of 4001 relevant lines covered (99.33%)

10896.33 hits per line

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

98.63
/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() : data_(0), resources_(0) {}
18✔
24

25
  // INTERNAL USE ONLY
26
  JsonArray(detail::ArrayData* data, detail::ResourceManager* resources)
332✔
27
      : data_(data), resources_(resources) {}
332✔
28

29
  // Returns a JsonVariant pointing to the array.
30
  // https://arduinojson.org/v7/api/jsonvariant/
31
  operator JsonVariant() {
8✔
32
    void* data = data_;  // prevent warning cast-align
8✔
33
    return JsonVariant(reinterpret_cast<detail::VariantData*>(data),
8✔
34
                       resources_);
8✔
35
  }
36

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

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

52
  // Appends a new (null) element to the array.
53
  // Returns a reference to the new element.
54
  // https://arduinojson.org/v7/api/jsonarray/add/
55
  template <typename T, detail::enable_if_t<
56
                            detail::is_same<T, JsonVariant>::value, int> = 0>
57
  JsonVariant add() const {
65✔
58
    return JsonVariant(detail::ArrayData::addElement(data_, resources_),
65✔
59
                       resources_);
65✔
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,545✔
66
    return detail::ArrayData::addValue(data_, value, resources_);
66,545✔
67
  }
68

69
  // Appends a value to the array.
70
  // https://arduinojson.org/v7/api/jsonarray/add/
71
  template <typename T,
72
            detail::enable_if_t<!detail::is_const<T>::value, int> = 0>
73
  bool add(T* value) const {
3✔
74
    return detail::ArrayData::addValue(data_, value, resources_);
3✔
75
  }
76

77
  // Returns an iterator to the first element of the array.
78
  // https://arduinojson.org/v7/api/jsonarray/begin/
79
  iterator begin() const {
7✔
80
    if (!data_)
7✔
81
      return iterator();
2✔
82
    return iterator(data_->createIterator(resources_), resources_);
5✔
83
  }
84

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

91
  // Copies an array.
92
  // https://arduinojson.org/v7/api/jsonarray/set/
93
  bool set(JsonArrayConst src) const {
13✔
94
    if (!data_)
13✔
95
      return false;
×
96

97
    clear();
13✔
98
    for (auto element : src) {
19✔
99
      if (!add(element))
7✔
100
        return false;
1✔
101
    }
102

103
    return true;
12✔
104
  }
105

106
  // Removes the element at the specified iterator.
107
  // https://arduinojson.org/v7/api/jsonarray/remove/
108
  void remove(iterator it) const {
6✔
109
    detail::ArrayData::remove(data_, it.iterator_, resources_);
6✔
110
  }
6✔
111

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

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

127
  // Removes all the elements of the array.
128
  // https://arduinojson.org/v7/api/jsonarray/clear/
129
  void clear() const {
16✔
130
    detail::ArrayData::clear(data_, resources_);
16✔
131
  }
16✔
132

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

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

152
  operator JsonVariantConst() const {
271✔
153
    return JsonVariantConst(collectionToVariant(data_), resources_);
271✔
154
  }
155

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

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

168
  // Returns the depth (nesting level) of the array.
169
  // https://arduinojson.org/v7/api/jsonarray/nesting/
170
  size_t nesting() const {
5✔
171
    return detail::VariantData::nesting(collectionToVariant(data_), resources_);
5✔
172
  }
173

174
  // Returns the number of elements in the array.
175
  // https://arduinojson.org/v7/api/jsonarray/size/
176
  size_t size() const {
79✔
177
    return data_ ? data_->size(resources_) : 0;
79✔
178
  }
179

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

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

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

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

202
 private:
203
  detail::ResourceManager* getResourceManager() const {
539✔
204
    return resources_;
539✔
205
  }
206

207
  detail::VariantData* getData() const {
245✔
208
    return collectionToVariant(data_);
245✔
209
  }
210

211
  detail::VariantData* getOrCreateData() const {
22✔
212
    return collectionToVariant(data_);
22✔
213
  }
214

215
  detail::ArrayData* data_;
216
  detail::ResourceManager* resources_;
217
};
218

219
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