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

bblanchon / ArduinoJson / 4427092977

pending completion
4427092977

push

github

Benoit Blanchon
Remove `StaticJsonDocument`

3300 of 3314 relevant lines covered (99.58%)

6184.45 hits per line

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

97.14
/src/ArduinoJson/Object/JsonObjectConst.hpp
1
// ArduinoJson - https://arduinojson.org
2
// Copyright © 2014-2023, Benoit BLANCHON
3
// MIT License
4

5
#pragma once
6

7
#include <ArduinoJson/Object/JsonObjectIterator.hpp>
8
#include <ArduinoJson/Variant/VariantOperators.hpp>
9

10
ARDUINOJSON_BEGIN_PUBLIC_NAMESPACE
11

12
// A read-only reference to an object in a JsonDocument.
13
// https://arduinojson.org/v6/api/jsonobjectconst/
14
class JsonObjectConst : public detail::VariantOperators<JsonObjectConst> {
15
  friend class JsonObject;
16
  friend class detail::VariantAttorney;
17

18
 public:
19
  typedef JsonObjectConstIterator iterator;
20

21
  // Creates an unbound reference.
22
  JsonObjectConst() : _data(0) {}
5✔
23

24
  // INTERNAL USE ONLY
25
  JsonObjectConst(const detail::CollectionData* data) : _data(data) {}
557✔
26

27
  operator JsonVariantConst() const {
68✔
28
    return JsonVariantConst(collectionToVariant(_data));
68✔
29
  }
30

31
  // Returns true if the reference is unbound.
32
  // https://arduinojson.org/v6/api/jsonobjectconst/isnull/
33
  FORCE_INLINE bool isNull() const {
34
    return _data == 0;
3✔
35
  }
36

37
  // Returns true if the reference is bound.
38
  // https://arduinojson.org/v6/api/jsonobjectconst/isnull/
39
  FORCE_INLINE operator bool() const {
40
    return _data != 0;
2✔
41
  }
42

43
  // Returns the number of bytes occupied by the object.
44
  // https://arduinojson.org/v6/api/jsonobjectconst/memoryusage/
45
  FORCE_INLINE size_t memoryUsage() const {
46
    return _data ? _data->memoryUsage() : 0;
47
  }
48

49
  // Returns the depth (nesting level) of the object.
50
  // https://arduinojson.org/v6/api/jsonobjectconst/nesting/
51
  FORCE_INLINE size_t nesting() const {
52
    return variantNesting(collectionToVariant(_data));
53
  }
54

55
  // Returns the number of members in the object.
56
  // https://arduinojson.org/v6/api/jsonobjectconst/size/
57
  FORCE_INLINE size_t size() const {
58
    return _data ? _data->size() : 0;
138✔
59
  }
60

61
  // Returns an iterator to the first key-value pair of the object.
62
  // https://arduinojson.org/v6/api/jsonobjectconst/begin/
63
  FORCE_INLINE iterator begin() const {
64
    if (!_data)
202✔
65
      return iterator();
1✔
66
    return iterator(_data->head());
201✔
67
  }
68

69
  // Returns an iterator following the last key-value pair of the object.
70
  // https://arduinojson.org/v6/api/jsonobjectconst/end/
71
  FORCE_INLINE iterator end() const {
72
    return iterator();
452✔
73
  }
74

75
  // Returns true if the object contains the specified key.
76
  // https://arduinojson.org/v6/api/jsonobjectconst/containskey/
77
  template <typename TString>
78
  FORCE_INLINE bool containsKey(const TString& key) const {
79
    return getMember(detail::adaptString(key)) != 0;
80
  }
81

82
  // Returns true if the object contains the specified key.
83
  // https://arduinojson.org/v6/api/jsonobjectconst/containskey/
84
  template <typename TChar>
85
  FORCE_INLINE bool containsKey(TChar* key) const {
86
    return getMember(detail::adaptString(key)) != 0;
2✔
87
  }
88

89
  // Gets the member with specified key.
90
  // https://arduinojson.org/v6/api/jsonobjectconst/subscript/
91
  template <typename TString>
92
  FORCE_INLINE typename detail::enable_if<detail::IsString<TString>::value,
93
                                          JsonVariantConst>::type
94
  operator[](const TString& key) const {
95
    return JsonVariantConst(getMember(detail::adaptString(key)));
308✔
96
  }
97

98
  // Gets the member with specified key.
99
  // https://arduinojson.org/v6/api/jsonobjectconst/subscript/
100
  template <typename TChar>
101
  FORCE_INLINE typename detail::enable_if<detail::IsString<TChar*>::value,
102
                                          JsonVariantConst>::type
103
  operator[](TChar* key) const {
104
    return JsonVariantConst(getMember(detail::adaptString(key)));
105
  }
106

107
  // Compares objects.
108
  FORCE_INLINE bool operator==(JsonObjectConst rhs) const {
109
    if (_data == rhs._data)
267✔
110
      return true;
61✔
111

112
    if (!_data || !rhs._data)
206✔
113
      return false;
6✔
114

115
    size_t count = 0;
200✔
116
    for (iterator it = begin(); it != end(); ++it) {
646✔
117
      if (it->value() != rhs[it->key()])
616✔
118
        return false;
62✔
119
      count++;
246✔
120
    }
121
    return count == rhs.size();
138✔
122
  }
123

124
 private:
125
  const detail::VariantData* getData() const {
68✔
126
    return collectionToVariant(_data);
68✔
127
  }
128

129
  template <typename TAdaptedString>
130
  const detail::VariantData* getMember(TAdaptedString key) const {
310✔
131
    if (!_data)
310✔
132
      return 0;
×
133
    return _data->getMember(key);
310✔
134
  }
135

136
  const detail::CollectionData* _data;
137
};
138

139
template <>
140
struct Converter<JsonObjectConst> : private detail::VariantAttorney {
141
  static void toJson(JsonVariantConst src, JsonVariant dst) {
142
    variantCopyFrom(getData(dst), getData(src), getPool(dst));
143
  }
144

145
  static JsonObjectConst fromJson(JsonVariantConst src) {
1✔
146
    auto data = getData(src);
1✔
147
    return data != 0 ? data->asObject() : 0;
1✔
148
  }
149

150
  static bool checkJson(JsonVariantConst src) {
304✔
151
    auto data = getData(src);
304✔
152
    return data && data->isObject();
304✔
153
  }
154
};
155

156
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