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

bblanchon / ArduinoJson / 5641653306

pending completion
5641653306

push

github

bblanchon
friend member

32 of 32 new or added lines in 4 files covered. (100.0%)

3444 of 3458 relevant lines covered (99.6%)

8764.75 hits per line

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

97.92
/src/ArduinoJson/Array/JsonArray.hpp
1
// ArduinoJson - https://arduinojson.org
2
// Copyright © 2014-2023, 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/v6/api/jsonarray/
16
class JsonArray : public detail::VariantOperators<JsonArray> {
17
  friend class detail::VariantAttorney;
18

19
 public:
20
  typedef JsonArrayIterator iterator;
21

22
  // Constructs an unbound reference.
23
  FORCE_INLINE JsonArray() : data_(0), resources_(0) {}
19✔
24

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

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

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

44
  // Appends a new (null) element to the array.
45
  // Returns a reference to the new element.
46
  // https://arduinojson.org/v6/api/jsonarray/add/
47
  JsonVariant add() const {
66,210✔
48
    return JsonVariant(detail::ArrayData::addElement(data_, resources_),
66,210✔
49
                       resources_);
132,420✔
50
  }
51

52
  // Appends a value to the array.
53
  // https://arduinojson.org/v6/api/jsonarray/add/
54
  template <typename T>
55
  FORCE_INLINE bool add(const T& value) const {
56
    return add().set(value);
1,081✔
57
  }
58

59
  // Appends a value to the array.
60
  // https://arduinojson.org/v6/api/jsonarray/add/
61
  template <typename T>
62
  FORCE_INLINE bool add(T* value) const {
63
    return add().set(value);
131,206✔
64
  }
65

66
  // Returns an iterator to the first element of the array.
67
  // https://arduinojson.org/v6/api/jsonarray/begin/
68
  FORCE_INLINE iterator begin() const {
69
    if (!data_)
7✔
70
      return iterator();
2✔
71
    return iterator(data_->createIterator(resources_), resources_);
5✔
72
  }
73

74
  // Returns an iterator following the last element of the array.
75
  // https://arduinojson.org/v6/api/jsonarray/end/
76
  FORCE_INLINE iterator end() const {
77
    return iterator();
6✔
78
  }
79

80
  // Copies an array.
81
  // https://arduinojson.org/v6/api/jsonarray/set/
82
  FORCE_INLINE bool set(JsonArrayConst src) const {
83
    if (!data_)
13✔
84
      return false;
×
85

86
    clear();
13✔
87
    for (auto element : src) {
31✔
88
      if (!add(element))
6✔
89
        return false;
1✔
90
    }
91

92
    return true;
12✔
93
  }
94

95
  // Removes the element at the specified iterator.
96
  // ⚠️ Doesn't release the memory associated with the removed element.
97
  // https://arduinojson.org/v6/api/jsonarray/remove/
98
  FORCE_INLINE void remove(iterator it) const {
99
    detail::ArrayData::remove(data_, it.iterator_, resources_);
5✔
100
  }
5✔
101

102
  // Removes the element at the specified index.
103
  // ⚠️ Doesn't release the memory associated with the removed element.
104
  // https://arduinojson.org/v6/api/jsonarray/remove/
105
  FORCE_INLINE void remove(size_t index) const {
106
    detail::ArrayData::removeElement(data_, index, resources_);
5✔
107
  }
5✔
108

109
  // Removes all the elements of the array.
110
  // ⚠️ Doesn't release the memory associated with the removed elements.
111
  // https://arduinojson.org/v6/api/jsonarray/clear/
112
  void clear() const {
16✔
113
    detail::ArrayData::clear(data_, resources_);
16✔
114
  }
16✔
115

116
  // Gets or sets the element at the specified index.
117
  // https://arduinojson.org/v6/api/jsonarray/subscript/
118
  FORCE_INLINE detail::ElementProxy<JsonArray> operator[](size_t index) const {
119
    return {*this, index};
232✔
120
  }
121

122
  // Creates an object and appends it to the array.
123
  // https://arduinojson.org/v6/api/jsonarray/createnestedobject/
124
  FORCE_INLINE JsonObject createNestedObject() const;
125

126
  // Creates an array and appends it to the array.
127
  // https://arduinojson.org/v6/api/jsonarray/createnestedarray/
128
  FORCE_INLINE JsonArray createNestedArray() const {
129
    return add().to<JsonArray>();
18✔
130
  }
131

132
  operator JsonVariantConst() const {
272✔
133
    return JsonVariantConst(collectionToVariant(data_), resources_);
272✔
134
  }
135

136
  // Returns true if the reference is unbound.
137
  // https://arduinojson.org/v6/api/jsonarray/isnull/
138
  FORCE_INLINE bool isNull() const {
139
    return data_ == 0;
25✔
140
  }
141

142
  // Returns true if the reference is bound.
143
  // https://arduinojson.org/v6/api/jsonarray/isnull/
144
  FORCE_INLINE operator bool() const {
145
    return data_ != 0;
2✔
146
  }
147

148
  // Returns the number of bytes occupied by the array.
149
  // https://arduinojson.org/v6/api/jsonarray/memoryusage/
150
  FORCE_INLINE size_t memoryUsage() const {
151
    return data_ ? data_->memoryUsage(resources_) : 0;
6✔
152
  }
153

154
  // Returns the depth (nesting level) of the array.
155
  // https://arduinojson.org/v6/api/jsonarray/nesting/
156
  FORCE_INLINE size_t nesting() const {
157
    return detail::VariantData::nesting(collectionToVariant(data_), resources_);
5✔
158
  }
159

160
  // Returns the number of elements in the array.
161
  // https://arduinojson.org/v6/api/jsonarray/size/
162
  FORCE_INLINE size_t size() const {
163
    return data_ ? data_->size(resources_) : 0;
76✔
164
  }
165

166
 private:
167
  detail::ResourceManager* getResourceManager() const {
512✔
168
    return resources_;
512✔
169
  }
170

171
  detail::VariantData* getData() const {
225✔
172
    return collectionToVariant(data_);
225✔
173
  }
174

175
  detail::VariantData* getOrCreateData() const {
23✔
176
    return collectionToVariant(data_);
23✔
177
  }
178

179
  detail::ArrayData* data_;
180
  detail::ResourceManager* resources_;
181
};
182

183
ARDUINOJSON_END_PUBLIC_NAMESPACE
184

185
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
186

187
template <typename TDerived>
188
template <typename T>
189
inline typename enable_if<is_same<T, JsonArray>::value, JsonArray>::type
190
VariantRefBase<TDerived>::to() const {
234✔
191
  return JsonArray(
192
      VariantData::toArray(getOrCreateData(), getResourceManager()),
193
      getResourceManager());
702✔
194
}
195

196
ARDUINOJSON_END_PRIVATE_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