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

bblanchon / ArduinoJson / 4466978960

pending completion
4466978960

push

github

Benoit Blanchon
Merge `DynamicJsonDocument` with `JsonDocument`

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

3279 of 3296 relevant lines covered (99.48%)

6214.2 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-2023, Benoit BLANCHON
3
// MIT License
4

5
#pragma once
6

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

10
ARDUINOJSON_BEGIN_PUBLIC_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 detail::VariantOperators<JsonObject> {
17
  friend class detail::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(detail::MemoryPool* buf, detail::CollectionData* data)
27
      : _data(data), _pool(buf) {}
383✔
28

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

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

38
  operator JsonVariantConst() const {
153✔
39
    return JsonVariantConst(collectionToVariant(_data));
153✔
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;
8✔
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
112
      typename detail::enable_if<detail::IsString<TString>::value,
113
                                 detail::MemberProxy<JsonObject, TString>>::type
114
      operator[](const TString& key) const {
115
    return {*this, key};
29✔
116
  }
117

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

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

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

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

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

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

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

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

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

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

195
 private:
196
  detail::MemoryPool* getPool() const {
951✔
197
    return _pool;
951✔
198
  }
199

200
  detail::VariantData* getData() const {
234✔
201
    return detail::collectionToVariant(_data);
234✔
202
  }
203

204
  detail::VariantData* getOrCreateData() const {
311✔
205
    return detail::collectionToVariant(_data);
311✔
206
  }
207

208
  template <typename TAdaptedString>
209
  inline detail::VariantData* getMember(TAdaptedString key) const {
6✔
210
    if (!_data)
6✔
211
      return 0;
×
212
    return _data->getMember(key);
6✔
213
  }
214

215
  template <typename TAdaptedString>
216
  void removeMember(TAdaptedString key) const {
13✔
217
    if (!_data)
13✔
218
      return;
1✔
219
    _data->removeMember(key);
12✔
220
  }
221

222
  detail::CollectionData* _data;
223
  detail::MemoryPool* _pool;
224
};
225

226
template <>
227
struct Converter<JsonObject> : private detail::VariantAttorney {
228
  static void toJson(JsonVariantConst src, JsonVariant dst) {
11✔
229
    variantCopyFrom(getData(dst), getData(src), getPool(dst));
33✔
230
  }
11✔
231

232
  static JsonObject fromJson(JsonVariant src) {
109✔
233
    auto data = getData(src);
109✔
234
    auto pool = getPool(src);
109✔
235
    return JsonObject(pool, data != 0 ? data->asObject() : 0);
218✔
236
  }
237

238
  static detail::InvalidConversion<JsonVariantConst, JsonObject> fromJson(
239
      JsonVariantConst);
240

241
  static bool checkJson(JsonVariantConst) {
9✔
242
    return false;
9✔
243
  }
244

245
  static bool checkJson(JsonVariant src) {
52✔
246
    auto data = getData(src);
52✔
247
    return data && data->isObject();
52✔
248
  }
249
};
250

251
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