Coverage Report

Created: 2026-01-09 06:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/arduinojson/src/ArduinoJson/Object/JsonObjectConst.hpp
Line
Count
Source
1
// ArduinoJson - https://arduinojson.org
2
// Copyright © 2014-2025, 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/v7/api/jsonobjectconst/
14
class JsonObjectConst : public detail::VariantOperators<JsonObjectConst> {
15
  friend class JsonObject;
16
  friend class detail::VariantAttorney;
17
18
 public:
19
  using iterator = JsonObjectConstIterator;
20
21
  // Creates an unbound reference.
22
0
  JsonObjectConst() {}
23
24
  // INTERNAL USE ONLY
25
  JsonObjectConst(detail::VariantData* data, detail::ResourceManager* resources)
26
0
      : impl_(data, resources) {}
27
28
  // INTERNAL USE ONLY
29
0
  JsonObjectConst(const detail::VariantImpl& impl) : impl_(impl) {}
30
31
0
  operator JsonVariantConst() const {
32
0
    return JsonVariantConst(impl_.data(), impl_.resources());
33
0
  }
34
35
  // Returns true if the reference is unbound.
36
  // https://arduinojson.org/v7/api/jsonobjectconst/isnull/
37
0
  bool isNull() const {
38
0
    return impl_.isNull();
39
0
  }
40
41
  // Returns true if the reference is bound.
42
  // https://arduinojson.org/v7/api/jsonobjectconst/isnull/
43
0
  operator bool() const {
44
0
    return !isNull();
45
0
  }
46
47
  // Returns the depth (nesting level) of the object.
48
  // https://arduinojson.org/v7/api/jsonobjectconst/nesting/
49
0
  size_t nesting() const {
50
0
    return impl_.nesting();
51
0
  }
52
53
  // Returns the number of members in the object.
54
  // https://arduinojson.org/v7/api/jsonobjectconst/size/
55
0
  size_t size() const {
56
0
    return impl_.size();
57
0
  }
58
59
  // Returns an iterator to the first key-value pair of the object.
60
  // https://arduinojson.org/v7/api/jsonobjectconst/begin/
61
0
  iterator begin() const {
62
0
    return iterator(impl_.createIterator(), impl_.resources());
63
0
  }
64
65
  // Returns an iterator following the last key-value pair of the object.
66
  // https://arduinojson.org/v7/api/jsonobjectconst/end/
67
0
  iterator end() const {
68
0
    return iterator();
69
0
  }
70
71
  // DEPRECATED: use obj[key].is<T>() instead
72
  // https://arduinojson.org/v7/api/jsonobjectconst/containskey/
73
  template <typename TString,
74
            detail::enable_if_t<detail::IsString<TString>::value, int> = 0>
75
  ARDUINOJSON_DEPRECATED("use obj[key].is<T>() instead")
76
  bool containsKey(const TString& key) const {
77
    return impl_.getMember(detail::adaptString(key)) != 0;
78
  }
79
80
  // DEPRECATED: use obj["key"].is<T>() instead
81
  // https://arduinojson.org/v7/api/jsonobjectconst/containskey/
82
  template <typename TChar>
83
  ARDUINOJSON_DEPRECATED("use obj[\"key\"].is<T>() instead")
84
  bool containsKey(TChar* key) const {
85
    return impl_.getMember(detail::adaptString(key)) != 0;
86
  }
87
88
  // DEPRECATED: use obj[key].is<T>() instead
89
  // https://arduinojson.org/v7/api/jsonobjectconst/containskey/
90
  template <typename TVariant,
91
            detail::enable_if_t<detail::IsVariant<TVariant>::value, int> = 0>
92
  ARDUINOJSON_DEPRECATED("use obj[key].is<T>() instead")
93
  bool containsKey(const TVariant& key) const {
94
    return containsKey(key.template as<const char*>());
95
  }
96
97
  // Gets the member with specified key.
98
  // https://arduinojson.org/v7/api/jsonobjectconst/subscript/
99
  template <typename TString,
100
            detail::enable_if_t<detail::IsString<TString>::value, int> = 0>
101
0
  JsonVariantConst operator[](const TString& key) const {
102
0
    return JsonVariantConst(impl_.getMember(detail::adaptString(key)),
103
0
                            impl_.resources());
104
0
  }
105
106
  // Gets the member with specified key.
107
  // https://arduinojson.org/v7/api/jsonobjectconst/subscript/
108
  template <typename TChar,
109
            detail::enable_if_t<detail::IsString<TChar*>::value, int> = 0>
110
  JsonVariantConst operator[](TChar* key) const {
111
    return JsonVariantConst(impl_.getMember(detail::adaptString(key)),
112
                            impl_.resources());
113
  }
114
115
  // Gets the member with specified key.
116
  // https://arduinojson.org/v7/api/jsonobjectconst/subscript/
117
  template <typename TVariant,
118
            detail::enable_if_t<detail::IsVariant<TVariant>::value, int> = 0>
119
  JsonVariantConst operator[](const TVariant& key) const {
120
    if (key.template is<JsonString>())
121
      return operator[](key.template as<JsonString>());
122
    else
123
      return JsonVariantConst();
124
  }
125
126
  // DEPRECATED: always returns zero
127
  ARDUINOJSON_DEPRECATED("always returns zero")
128
0
  size_t memoryUsage() const {
129
0
    return 0;
130
0
  }
131
132
 private:
133
0
  const detail::VariantData* getData() const {
134
0
    return impl_.data();
135
0
  }
136
137
  detail::VariantImpl impl_;
138
};
139
140
0
inline bool operator==(JsonObjectConst lhs, JsonObjectConst rhs) {
141
0
  if (!lhs && !rhs)  // both are null
142
0
    return true;
143
0
144
0
  if (!lhs || !rhs)  // only one is null
145
0
    return false;
146
0
147
0
  size_t count = 0;
148
0
  for (auto kvp : lhs) {
149
0
    auto rhsValue = rhs[kvp.key()];
150
0
    if (rhsValue.isUnbound())
151
0
      return false;
152
0
    if (kvp.value() != rhsValue)
153
0
      return false;
154
0
    count++;
155
0
  }
156
0
  return count == rhs.size();
157
0
}
158
159
ARDUINOJSON_END_PUBLIC_NAMESPACE