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

bblanchon / ArduinoJson / 11879748170

17 Nov 2024 02:40PM CUT coverage: 99.327%. Remained the same
11879748170

push

github

bblanchon
Fix support for NUL characters in `deserializeJson()`

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

3985 of 4012 relevant lines covered (99.33%)

10799.38 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-2024, 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)
337✔
27
      : data_(data), resources_(resources) {}
337✔
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>
47
  detail::enable_if_t<!detail::is_same<T, JsonVariant>::value, T> add() const {
36✔
48
    return add<JsonVariant>().to<T>();
72✔
49
  }
50

51
  // Appends a new (null) element to the array.
52
  // Returns a reference to the new element.
53
  // https://arduinojson.org/v7/api/jsonarray/add/
54
  template <typename T>
55
  detail::enable_if_t<detail::is_same<T, JsonVariant>::value, T> add() const {
65✔
56
    return JsonVariant(detail::ArrayData::addElement(data_, resources_),
65✔
57
                       resources_);
65✔
58
  }
59

60
  // Appends a value to the array.
61
  // https://arduinojson.org/v7/api/jsonarray/add/
62
  template <typename T>
63
  bool add(const T& value) const {
943✔
64
    return detail::ArrayData::addValue(data_, value, resources_);
943✔
65
  }
66

67
  // Appends a value to the array.
68
  // https://arduinojson.org/v7/api/jsonarray/add/
69
  template <typename T>
70
  bool add(T* value) const {
65,609✔
71
    return detail::ArrayData::addValue(data_, value, resources_);
65,609✔
72
  }
73

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

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

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

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

100
    return true;
12✔
101
  }
102

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

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

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

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

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

139
  // Gets or sets the element at the specified index.
140
  // https://arduinojson.org/v7/api/jsonarray/subscript/
141
  template <typename TVariant>
142
  detail::enable_if_t<detail::IsVariant<TVariant>::value,
143
                      detail::ElementProxy<JsonArray>>
144
  operator[](const TVariant& variant) const {
2✔
145
    if (variant.template is<size_t>())
2✔
146
      return operator[](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(collectionToVariant(data_), resources_);
271✔
153
  }
154

155
  // Returns true if the reference is unbound.
156
  // https://arduinojson.org/v7/api/jsonarray/isnull/
157
  bool isNull() const {
19✔
158
    return data_ == 0;
19✔
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 data_ != 0;
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 detail::VariantData::nesting(collectionToVariant(data_), resources_);
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 data_ ? data_->size(resources_) : 0;
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 {
528✔
203
    return resources_;
528✔
204
  }
205

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

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

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

218
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