|
7 | 7 |
|
8 | 8 | #pragma once
|
9 | 9 |
|
10 |
| -#include "Data/JsonVariantAs.hpp" |
11 |
| -#include "Polyfills/attributes.hpp" |
| 10 | +#include "JsonVariantCasts.hpp" |
| 11 | +#include "JsonVariantComparisons.hpp" |
| 12 | +#include "JsonVariantSubscripts.hpp" |
12 | 13 | #include "Serialization/JsonPrintable.hpp"
|
13 | 14 |
|
14 | 15 | namespace ArduinoJson {
|
15 | 16 |
|
16 |
| -// Forward declarations. |
17 |
| -class JsonArraySubscript; |
18 |
| -template <typename TKey> |
19 |
| -class JsonObjectSubscript; |
20 |
| - |
21 | 17 | template <typename TImpl>
|
22 |
| -class JsonVariantBase : public Internals::JsonPrintable<TImpl> { |
23 |
| - public: |
24 |
| -#if ARDUINOJSON_ENABLE_DEPRECATED |
25 |
| - DEPRECATED("use as<JsonArray>() instead") |
26 |
| - FORCE_INLINE JsonArray &asArray() const { |
27 |
| - return as<JsonArray>(); |
28 |
| - } |
29 |
| - |
30 |
| - DEPRECATED("use as<JsonObject>() instead") |
31 |
| - FORCE_INLINE JsonObject &asObject() const { |
32 |
| - return as<JsonObject>(); |
33 |
| - } |
34 |
| - |
35 |
| - DEPRECATED("use as<char*>() instead") |
36 |
| - FORCE_INLINE const char *asString() const { |
37 |
| - return as<const char *>(); |
38 |
| - } |
39 |
| -#endif |
40 |
| - |
41 |
| - // Gets the variant as an array. |
42 |
| - // Returns a reference to the JsonArray or JsonArray::invalid() if the |
43 |
| - // variant |
44 |
| - // is not an array. |
45 |
| - FORCE_INLINE operator JsonArray &() const { |
46 |
| - return as<JsonArray &>(); |
47 |
| - } |
48 |
| - |
49 |
| - // Gets the variant as an object. |
50 |
| - // Returns a reference to the JsonObject or JsonObject::invalid() if the |
51 |
| - // variant is not an object. |
52 |
| - FORCE_INLINE operator JsonObject &() const { |
53 |
| - return as<JsonObject &>(); |
54 |
| - } |
55 |
| - |
56 |
| - template <typename T> |
57 |
| - FORCE_INLINE operator T() const { |
58 |
| - return as<T>(); |
59 |
| - } |
60 |
| - |
61 |
| - template <typename T> |
62 |
| - FORCE_INLINE const typename Internals::JsonVariantAs<T>::type as() const { |
63 |
| - return impl()->template as<T>(); |
64 |
| - } |
65 |
| - |
66 |
| - template <typename T> |
67 |
| - FORCE_INLINE bool is() const { |
68 |
| - return impl()->template is<T>(); |
69 |
| - } |
70 |
| - |
71 |
| - // Mimics an array or an object. |
72 |
| - // Returns the size of the array or object if the variant has that type. |
73 |
| - // Returns 0 if the variant is neither an array nor an object |
74 |
| - size_t size() const { |
75 |
| - return as<JsonArray>().size() + as<JsonObject>().size(); |
76 |
| - } |
77 |
| - |
78 |
| - // Mimics an array. |
79 |
| - // Returns the element at specified index if the variant is an array. |
80 |
| - // Returns JsonVariant::invalid() if the variant is not an array. |
81 |
| - FORCE_INLINE const JsonArraySubscript operator[](size_t index) const; |
82 |
| - FORCE_INLINE JsonArraySubscript operator[](size_t index); |
83 |
| - |
84 |
| - // Mimics an object. |
85 |
| - // Returns the value associated with the specified key if the variant is |
86 |
| - // an object. |
87 |
| - // Return JsonVariant::invalid() if the variant is not an object. |
88 |
| - // |
89 |
| - // const JsonObjectSubscript operator[](TKey) const; |
90 |
| - // TKey = const std::string&, const String& |
91 |
| - template <typename TString> |
92 |
| - FORCE_INLINE typename TypeTraits::EnableIf< |
93 |
| - Internals::StringTraits<TString>::has_equals, |
94 |
| - const JsonObjectSubscript<const TString &> >::type |
95 |
| - operator[](const TString &key) const { |
96 |
| - return as<JsonObject>()[key]; |
97 |
| - } |
98 |
| - // |
99 |
| - // const JsonObjectSubscript operator[](TKey) const; |
100 |
| - // TKey = const std::string&, const String& |
101 |
| - template <typename TString> |
102 |
| - FORCE_INLINE typename TypeTraits::EnableIf< |
103 |
| - Internals::StringTraits<TString>::has_equals, |
104 |
| - JsonObjectSubscript<const TString &> >::type |
105 |
| - operator[](const TString &key) { |
106 |
| - return as<JsonObject>()[key]; |
107 |
| - } |
108 |
| - // |
109 |
| - // JsonObjectSubscript operator[](TKey); |
110 |
| - // TKey = const char*, const char[N], const FlashStringHelper* |
111 |
| - template <typename TString> |
112 |
| - FORCE_INLINE typename TypeTraits::EnableIf< |
113 |
| - Internals::StringTraits<const TString *>::has_equals, |
114 |
| - JsonObjectSubscript<const TString *> >::type |
115 |
| - operator[](const TString *key) { |
116 |
| - return as<JsonObject>()[key]; |
117 |
| - } |
118 |
| - // |
119 |
| - // JsonObjectSubscript operator[](TKey); |
120 |
| - // TKey = const char*, const char[N], const FlashStringHelper* |
121 |
| - template <typename TString> |
122 |
| - FORCE_INLINE typename TypeTraits::EnableIf< |
123 |
| - Internals::StringTraits<TString *>::has_equals, |
124 |
| - const JsonObjectSubscript<const TString *> >::type |
125 |
| - operator[](const TString *key) const { |
126 |
| - return as<JsonObject>()[key]; |
127 |
| - } |
128 |
| - |
129 |
| - private: |
130 |
| - const TImpl *impl() const { |
131 |
| - return static_cast<const TImpl *>(this); |
132 |
| - } |
133 |
| -}; |
134 |
| - |
135 |
| -namespace TypeTraits { |
136 |
| -template <typename T> |
137 |
| -struct IsVariant : IsBaseOf<JsonVariantBase<T>, T> {}; |
138 |
| -} |
| 18 | +class JsonVariantBase : public Internals::JsonPrintable<TImpl>, |
| 19 | + public JsonVariantCasts<TImpl>, |
| 20 | + public JsonVariantComparisons<TImpl>, |
| 21 | + public JsonVariantSubscripts<TImpl>, |
| 22 | + public TypeTraits::JsonVariantTag {}; |
139 | 23 | }
|
0 commit comments