/src/arduinojson/src/ArduinoJson/Variant/JsonVariantConst.hpp
Line | Count | Source (jump to first uncovered line) |
1 | | // ArduinoJson - https://arduinojson.org |
2 | | // Copyright © 2014-2025, Benoit BLANCHON |
3 | | // MIT License |
4 | | |
5 | | #pragma once |
6 | | |
7 | | #include <stddef.h> |
8 | | #include <stdint.h> // for uint8_t |
9 | | |
10 | | #include <ArduinoJson/Memory/ResourceManager.hpp> |
11 | | #include <ArduinoJson/Polyfills/type_traits.hpp> |
12 | | #include <ArduinoJson/Strings/IsString.hpp> |
13 | | #include <ArduinoJson/Strings/StringAdapters.hpp> |
14 | | #include <ArduinoJson/Variant/VariantAttorney.hpp> |
15 | | #include <ArduinoJson/Variant/VariantOperators.hpp> |
16 | | #include <ArduinoJson/Variant/VariantTag.hpp> |
17 | | |
18 | | ARDUINOJSON_BEGIN_PUBLIC_NAMESPACE |
19 | | |
20 | | // Forward declarations. |
21 | | class JsonArray; |
22 | | class JsonObject; |
23 | | |
24 | | // A read-only reference to a value in a JsonDocument |
25 | | // https://arduinojson.org/v7/api/jsonarrayconst/ |
26 | | class JsonVariantConst : public detail::VariantTag, |
27 | | public detail::VariantOperators<JsonVariantConst> { |
28 | | friend class detail::VariantAttorney; |
29 | | |
30 | | template <typename T> |
31 | | using ConversionSupported = |
32 | | detail::is_same<typename detail::function_traits< |
33 | | decltype(&Converter<T>::fromJson)>::arg1_type, |
34 | | JsonVariantConst>; |
35 | | |
36 | | public: |
37 | | // Creates an unbound reference. |
38 | 0 | JsonVariantConst() : data_(nullptr), resources_(nullptr) {} |
39 | | |
40 | | // INTERNAL USE ONLY |
41 | | explicit JsonVariantConst(const detail::VariantData* data, |
42 | | const detail::ResourceManager* resources) |
43 | 947 | : data_(data), resources_(resources) {} |
44 | | |
45 | | // Returns true if the value is null or the reference is unbound. |
46 | | // https://arduinojson.org/v7/api/jsonvariantconst/isnull/ |
47 | 0 | bool isNull() const { |
48 | 0 | return detail::VariantData::isNull(data_); |
49 | 0 | } |
50 | | |
51 | | // Returns true if the reference is unbound. |
52 | 0 | bool isUnbound() const { |
53 | 0 | return !data_; |
54 | 0 | } |
55 | | |
56 | | // Returns the depth (nesting level) of the value. |
57 | | // https://arduinojson.org/v7/api/jsonvariantconst/nesting/ |
58 | 0 | size_t nesting() const { |
59 | 0 | return detail::VariantData::nesting(data_, resources_); |
60 | 0 | } |
61 | | |
62 | | // Returns the size of the array or object. |
63 | | // https://arduinojson.org/v7/api/jsonvariantconst/size/ |
64 | 0 | size_t size() const { |
65 | 0 | return detail::VariantData::size(data_, resources_); |
66 | 0 | } |
67 | | |
68 | | // Casts the value to the specified type. |
69 | | // https://arduinojson.org/v7/api/jsonvariantconst/as/ |
70 | | template <typename T, |
71 | | detail::enable_if_t<ConversionSupported<T>::value, int> = 0> |
72 | 0 | T as() const { |
73 | 0 | return Converter<T>::fromJson(*this); |
74 | 0 | } Unexecuted instantiation: _ZNK11ArduinoJson8V730HB4216JsonVariantConst2asIS1_TnNS0_6detail9enable_ifIXsr19ConversionSupportedIT_EE5valueEiE4typeELi0EEES5_v Unexecuted instantiation: _ZNK11ArduinoJson8V730HB4216JsonVariantConst2asINS0_10JsonStringETnNS0_6detail9enable_ifIXsr19ConversionSupportedIT_EE5valueEiE4typeELi0EEES6_v Unexecuted instantiation: _ZNK11ArduinoJson8V730HB4216JsonVariantConst2asINS0_14JsonArrayConstETnNS0_6detail9enable_ifIXsr19ConversionSupportedIT_EE5valueEiE4typeELi0EEES6_v Unexecuted instantiation: _ZNK11ArduinoJson8V730HB4216JsonVariantConst2asINS0_15JsonObjectConstETnNS0_6detail9enable_ifIXsr19ConversionSupportedIT_EE5valueEiE4typeELi0EEES6_v Unexecuted instantiation: _ZNK11ArduinoJson8V730HB4216JsonVariantConst2asIbTnNS0_6detail9enable_ifIXsr19ConversionSupportedIT_EE5valueEiE4typeELi0EEES5_v |
75 | | |
76 | | // Invalid conversion. Will not compile. |
77 | | template <typename T, |
78 | | detail::enable_if_t<!ConversionSupported<T>::value, int> = 0> |
79 | | detail::InvalidConversion<JsonVariantConst, T> as() const; |
80 | | |
81 | | // Returns true if the value is of the specified type. |
82 | | // https://arduinojson.org/v7/api/jsonvariantconst/is/ |
83 | | template <typename T, |
84 | | detail::enable_if_t<ConversionSupported<T>::value, int> = 0> |
85 | 0 | bool is() const { |
86 | 0 | return Converter<T>::checkJson(*this); |
87 | 0 | } Unexecuted instantiation: _ZNK11ArduinoJson8V730HB4216JsonVariantConst2isINS0_10JsonStringETnNS0_6detail9enable_ifIXsr19ConversionSupportedIT_EE5valueEiE4typeELi0EEEbv Unexecuted instantiation: _ZNK11ArduinoJson8V730HB4216JsonVariantConst2isINS0_14JsonArrayConstETnNS0_6detail9enable_ifIXsr19ConversionSupportedIT_EE5valueEiE4typeELi0EEEbv Unexecuted instantiation: _ZNK11ArduinoJson8V730HB4216JsonVariantConst2isINS0_15JsonObjectConstETnNS0_6detail9enable_ifIXsr19ConversionSupportedIT_EE5valueEiE4typeELi0EEEbv |
88 | | |
89 | | // Always returns false for the unsupported types. |
90 | | // https://arduinojson.org/v7/api/jsonvariantconst/is/ |
91 | | template <typename T, |
92 | | detail::enable_if_t<!ConversionSupported<T>::value, int> = 0> |
93 | | bool is() const { |
94 | | return false; |
95 | | } |
96 | | |
97 | | template <typename T> |
98 | 0 | operator T() const { |
99 | 0 | return as<T>(); |
100 | 0 | } Unexecuted instantiation: ArduinoJson::V730HB42::JsonVariantConst::operator ArduinoJson::V730HB42::JsonArrayConst<ArduinoJson::V730HB42::JsonArrayConst>() const Unexecuted instantiation: ArduinoJson::V730HB42::JsonVariantConst::operator ArduinoJson::V730HB42::JsonObjectConst<ArduinoJson::V730HB42::JsonObjectConst>() const Unexecuted instantiation: ArduinoJson::V730HB42::JsonVariantConst::operator bool<bool>() const |
101 | | |
102 | | // Gets array's element at specified index. |
103 | | // https://arduinojson.org/v7/api/jsonvariantconst/subscript/ |
104 | | template <typename T, |
105 | | detail::enable_if_t<detail::is_integral<T>::value, int> = 0> |
106 | | JsonVariantConst operator[](T index) const { |
107 | | return JsonVariantConst( |
108 | | detail::VariantData::getElement(data_, size_t(index), resources_), |
109 | | resources_); |
110 | | } |
111 | | |
112 | | // Gets object's member with specified key. |
113 | | // https://arduinojson.org/v7/api/jsonvariantconst/subscript/ |
114 | | template <typename TString, |
115 | | detail::enable_if_t<detail::IsString<TString>::value, int> = 0> |
116 | | JsonVariantConst operator[](const TString& key) const { |
117 | | return JsonVariantConst(detail::VariantData::getMember( |
118 | | data_, detail::adaptString(key), resources_), |
119 | | resources_); |
120 | | } |
121 | | |
122 | | // Gets object's member with specified key. |
123 | | // https://arduinojson.org/v7/api/jsonvariantconst/subscript/ |
124 | | template <typename TChar, |
125 | | detail::enable_if_t<detail::IsString<TChar*>::value && |
126 | | !detail::is_const<TChar>::value, |
127 | | int> = 0> |
128 | | JsonVariantConst operator[](TChar* key) const { |
129 | | return JsonVariantConst(detail::VariantData::getMember( |
130 | | data_, detail::adaptString(key), resources_), |
131 | | resources_); |
132 | | } |
133 | | |
134 | | // Gets object's member with specified key or the array's element at the |
135 | | // specified index. |
136 | | // https://arduinojson.org/v7/api/jsonvariantconst/subscript/ |
137 | | template <typename TVariant, |
138 | | detail::enable_if_t<detail::IsVariant<TVariant>::value, int> = 0> |
139 | | JsonVariantConst operator[](const TVariant& key) const { |
140 | | if (key.template is<size_t>()) |
141 | | return operator[](key.template as<size_t>()); |
142 | | else |
143 | | return operator[](key.template as<JsonString>()); |
144 | | } |
145 | | |
146 | | // DEPRECATED: use obj[key].is<T>() instead |
147 | | // https://arduinojson.org/v7/api/jsonvariantconst/containskey/ |
148 | | template <typename TString, |
149 | | detail::enable_if_t<detail::IsString<TString>::value, int> = 0> |
150 | | ARDUINOJSON_DEPRECATED("use var[key].is<T>() instead") |
151 | | bool containsKey(const TString& key) const { |
152 | | return detail::VariantData::getMember(getData(), detail::adaptString(key), |
153 | | resources_) != 0; |
154 | | } |
155 | | |
156 | | // DEPRECATED: use obj["key"].is<T>() instead |
157 | | // https://arduinojson.org/v7/api/jsonvariantconst/containskey/ |
158 | | template <typename TChar, |
159 | | detail::enable_if_t<detail::IsString<TChar*>::value && |
160 | | !detail::is_const<TChar>::value, |
161 | | int> = 0> |
162 | | ARDUINOJSON_DEPRECATED("use obj[\"key\"].is<T>() instead") |
163 | | bool containsKey(TChar* key) const { |
164 | | return detail::VariantData::getMember(getData(), detail::adaptString(key), |
165 | | resources_) != 0; |
166 | | } |
167 | | |
168 | | // DEPRECATED: use obj[key].is<T>() instead |
169 | | // https://arduinojson.org/v7/api/jsonvariantconst/containskey/ |
170 | | template <typename TVariant, |
171 | | detail::enable_if_t<detail::IsVariant<TVariant>::value, int> = 0> |
172 | | ARDUINOJSON_DEPRECATED("use var[key].is<T>() instead") |
173 | | bool containsKey(const TVariant& key) const { |
174 | | return containsKey(key.template as<const char*>()); |
175 | | } |
176 | | |
177 | | // DEPRECATED: always returns zero |
178 | | ARDUINOJSON_DEPRECATED("always returns zero") |
179 | 0 | size_t memoryUsage() const { |
180 | 0 | return 0; |
181 | 0 | } |
182 | | |
183 | | protected: |
184 | 947 | const detail::VariantData* getData() const { |
185 | 947 | return data_; |
186 | 947 | } |
187 | | |
188 | 947 | const detail::ResourceManager* getResourceManager() const { |
189 | 947 | return resources_; |
190 | 947 | } |
191 | | |
192 | | private: |
193 | | const detail::VariantData* data_; |
194 | | const detail::ResourceManager* resources_; |
195 | | }; |
196 | | |
197 | | ARDUINOJSON_END_PUBLIC_NAMESPACE |