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

bblanchon / ArduinoJson / 4143639874

pending completion
4143639874

push

github

Benoit Blanchon
Remove `ARDUINOJSON_HAS_NULLPTR`

3390 of 3406 relevant lines covered (99.53%)

5984.83 hits per line

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

98.51
/src/ArduinoJson/Object/JsonObject.hpp
1
// ArduinoJson - https://arduinojson.org
2
// Copyright © 2014-2022, Benoit BLANCHON
3
// MIT License
4

5
#pragma once
6

7
#include <ArduinoJson/Object/JsonObjectConst.hpp>
8
#include <ArduinoJson/Object/MemberProxy.hpp>
9

10
namespace ARDUINOJSON_NAMESPACE {
11

12
class JsonArray;
13

14
// A reference to an object in a JsonDocument.
15
// https://arduinojson.org/v6/api/jsonobject/
16
class JsonObject : public VariantOperators<JsonObject> {
17
  friend class VariantAttorney;
18

19
 public:
20
  typedef JsonObjectIterator iterator;
21

22
  // Creates an unbound reference.
23
  FORCE_INLINE JsonObject() : _data(0), _pool(0) {}
20✔
24

25
  // INTERNAL USE ONLY
26
  FORCE_INLINE JsonObject(MemoryPool* buf, CollectionData* data)
27
      : _data(data), _pool(buf) {}
386✔
28

29
  operator JsonVariant() const {
8✔
30
    void* data = _data;  // prevent warning cast-align
8✔
31
    return JsonVariant(_pool, reinterpret_cast<VariantData*>(data));
8✔
32
  }
33

34
  operator JsonObjectConst() const {
40✔
35
    return JsonObjectConst(_data);
40✔
36
  }
37

38
  operator JsonVariantConst() const {
155✔
39
    return JsonVariantConst(collectionToVariant(_data));
155✔
40
  }
41

42
  // Returns true if the reference is unbound.
43
  // https://arduinojson.org/v6/api/jsonobject/isnull/
44
  FORCE_INLINE bool isNull() const {
45
    return _data == 0;
13✔
46
  }
47

48
  // Returns true if the reference is bound.
49
  // https://arduinojson.org/v6/api/jsonobject/isnull/
50
  FORCE_INLINE operator bool() const {
51
    return _data != 0;
2✔
52
  }
53

54
  // Returns the number of bytes occupied by the object.
55
  // https://arduinojson.org/v6/api/jsonobject/memoryusage/
56
  FORCE_INLINE size_t memoryUsage() const {
57
    return _data ? _data->memoryUsage() : 0;
9✔
58
  }
59

60
  // Returns the depth (nesting level) of the object.
61
  // https://arduinojson.org/v6/api/jsonobject/nesting/
62
  FORCE_INLINE size_t nesting() const {
63
    return variantNesting(collectionToVariant(_data));
5✔
64
  }
65

66
  // Returns the number of members in the object.
67
  // https://arduinojson.org/v6/api/jsonobject/size/
68
  FORCE_INLINE size_t size() const {
69
    return _data ? _data->size() : 0;
44✔
70
  }
71

72
  // Returns an iterator to the first key-value pair of the object.
73
  // https://arduinojson.org/v6/api/jsonobject/begin/
74
  FORCE_INLINE iterator begin() const {
75
    if (!_data)
27✔
76
      return iterator();
2✔
77
    return iterator(_pool, _data->head());
25✔
78
  }
79

80
  // Returns an iterator following the last key-value pair of the object.
81
  // https://arduinojson.org/v6/api/jsonobject/end/
82
  FORCE_INLINE iterator end() const {
83
    return iterator();
6✔
84
  }
85

86
  // Removes all the members of the object.
87
  // ⚠️ Doesn't release the memory associated with the removed members.
88
  // https://arduinojson.org/v6/api/jsonobject/clear/
89
  void clear() const {
2✔
90
    if (!_data)
2✔
91
      return;
1✔
92
    _data->clear();
1✔
93
  }
94

95
  // Copies an object.
96
  // https://arduinojson.org/v6/api/jsonobject/set/
97
  FORCE_INLINE bool set(JsonObjectConst src) {
98
    if (!_data || !src._data)
10✔
99
      return false;
2✔
100
    return _data->copyFrom(*src._data, _pool);
8✔
101
  }
102

103
  // Compares the content of two objects.
104
  FORCE_INLINE bool operator==(JsonObject rhs) const {
105
    return JsonObjectConst(_data) == JsonObjectConst(rhs._data);
32✔
106
  }
107

108
  // Gets or sets the member with specified key.
109
  // https://arduinojson.org/v6/api/jsonobject/subscript/
110
  template <typename TString>
111
  FORCE_INLINE typename enable_if<IsString<TString>::value,
112
                                  MemberProxy<JsonObject, TString> >::type
113
  operator[](const TString& key) const {
114
    return MemberProxy<JsonObject, TString>(*this, key);
29✔
115
  }
116

117
  // Gets or sets the member with specified key.
118
  // https://arduinojson.org/v6/api/jsonobject/subscript/
119
  template <typename TChar>
120
  FORCE_INLINE typename enable_if<IsString<TChar*>::value,
121
                                  MemberProxy<JsonObject, TChar*> >::type
122
  operator[](TChar* key) const {
123
    return MemberProxy<JsonObject, TChar*>(*this, key);
438✔
124
  }
125

126
  // Removes the member at the specified iterator.
127
  // ⚠️ Doesn't release the memory associated with the removed member.
128
  // https://arduinojson.org/v6/api/jsonobject/remove/
129
  FORCE_INLINE void remove(iterator it) const {
130
    if (!_data)
4✔
131
      return;
1✔
132
    _data->removeSlot(it._slot);
3✔
133
  }
134

135
  // Removes the member with the specified key.
136
  // ⚠️ Doesn't release the memory associated with the removed member.
137
  // https://arduinojson.org/v6/api/jsonobject/remove/
138
  template <typename TString>
139
  FORCE_INLINE void remove(const TString& key) const {
140
    removeMember(adaptString(key));
1✔
141
  }
1✔
142

143
  // Removes the member with the specified key.
144
  // ⚠️ Doesn't release the memory associated with the removed member.
145
  // https://arduinojson.org/v6/api/jsonobject/remove/
146
  template <typename TChar>
147
  FORCE_INLINE void remove(TChar* key) const {
148
    removeMember(adaptString(key));
12✔
149
  }
12✔
150

151
  // Returns true if the object contains the specified key.
152
  // https://arduinojson.org/v6/api/jsonobject/containskey/
153
  template <typename TString>
154
  FORCE_INLINE typename enable_if<IsString<TString>::value, bool>::type
155
  containsKey(const TString& key) const {
156
    return getMember(adaptString(key)) != 0;
1✔
157
  }
158

159
  // Returns true if the object contains the specified key.
160
  // https://arduinojson.org/v6/api/jsonobject/containskey/
161
  template <typename TChar>
162
  FORCE_INLINE typename enable_if<IsString<TChar*>::value, bool>::type
163
  containsKey(TChar* key) const {
164
    return getMember(adaptString(key)) != 0;
5✔
165
  }
166

167
  // Creates an array and adds it to the object.
168
  // https://arduinojson.org/v6/api/jsonobject/createnestedarray/
169
  template <typename TString>
170
  FORCE_INLINE JsonArray createNestedArray(const TString& key) const;
171

172
  // Creates an array and adds it to the object.
173
  // https://arduinojson.org/v6/api/jsonobject/createnestedarray/
174
  template <typename TChar>
175
  FORCE_INLINE JsonArray createNestedArray(TChar* key) const;
176

177
  // Creates an object and adds it to the object.
178
  // https://arduinojson.org/v6/api/jsonobject/createnestedobject/
179
  template <typename TString>
180
  JsonObject createNestedObject(const TString& key) const {
1✔
181
    return operator[](key).template to<JsonObject>();
1✔
182
  }
183

184
  // Creates an object and adds it to the object.
185
  // https://arduinojson.org/v6/api/jsonobject/createnestedobject/
186
  template <typename TChar>
187
  JsonObject createNestedObject(TChar* key) const {
10✔
188
    return operator[](key).template to<JsonObject>();
10✔
189
  }
190

191
 private:
192
  MemoryPool* getPool() const {
954✔
193
    return _pool;
954✔
194
  }
195

196
  VariantData* getData() const {
234✔
197
    return collectionToVariant(_data);
234✔
198
  }
199

200
  VariantData* getOrCreateData() const {
312✔
201
    return collectionToVariant(_data);
312✔
202
  }
203

204
  template <typename TAdaptedString>
205
  inline VariantData* getMember(TAdaptedString key) const {
6✔
206
    if (!_data)
6✔
207
      return 0;
×
208
    return _data->getMember(key);
6✔
209
  }
210

211
  template <typename TAdaptedString>
212
  void removeMember(TAdaptedString key) const {
13✔
213
    if (!_data)
13✔
214
      return;
1✔
215
    _data->removeMember(key);
12✔
216
  }
217

218
  CollectionData* _data;
219
  MemoryPool* _pool;
220
};
221

222
template <>
223
struct Converter<JsonObject> : private VariantAttorney {
224
  static void toJson(JsonVariantConst src, JsonVariant dst) {
13✔
225
    variantCopyFrom(getData(dst), getData(src), getPool(dst));
39✔
226
  }
13✔
227

228
  static JsonObject fromJson(JsonVariant src) {
111✔
229
    VariantData* data = getData(src);
111✔
230
    MemoryPool* pool = getPool(src);
111✔
231
    return JsonObject(pool, data != 0 ? data->asObject() : 0);
222✔
232
  }
233

234
  static InvalidConversion<JsonVariantConst, JsonObject> fromJson(
235
      JsonVariantConst);
236

237
  static bool checkJson(JsonVariantConst) {
9✔
238
    return false;
9✔
239
  }
240

241
  static bool checkJson(JsonVariant src) {
52✔
242
    VariantData* data = getData(src);
52✔
243
    return data && data->isObject();
52✔
244
  }
245
};
246
}  // namespace ARDUINOJSON_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