[209] | 1 | // Copyright Benoit Blanchon 2014-2016
|
---|
| 2 | // MIT License
|
---|
| 3 | //
|
---|
| 4 | // Arduino JSON library
|
---|
| 5 | // https://github.com/bblanchon/ArduinoJson
|
---|
| 6 | // If you like this project, please add a star!
|
---|
| 7 |
|
---|
| 8 | #pragma once
|
---|
| 9 |
|
---|
| 10 | #include <stddef.h>
|
---|
| 11 | #include <stdint.h> // for uint8_t
|
---|
| 12 |
|
---|
| 13 | #include "Internals/JsonPrintable.hpp"
|
---|
| 14 | #include "Internals/JsonVariantContent.hpp"
|
---|
| 15 | #include "Internals/JsonVariantType.hpp"
|
---|
| 16 | #include "Internals/Unparsed.hpp"
|
---|
| 17 | #include "JsonVariantBase.hpp"
|
---|
| 18 | #include "TypeTraits/EnableIf.hpp"
|
---|
| 19 | #include "TypeTraits/IsFloatingPoint.hpp"
|
---|
| 20 | #include "TypeTraits/IsIntegral.hpp"
|
---|
| 21 | #include "TypeTraits/IsSame.hpp"
|
---|
| 22 | #include "TypeTraits/RemoveConst.hpp"
|
---|
| 23 | #include "TypeTraits/RemoveReference.hpp"
|
---|
| 24 |
|
---|
| 25 | namespace ArduinoJson {
|
---|
| 26 |
|
---|
| 27 | // Forward declarations.
|
---|
| 28 | class JsonArray;
|
---|
| 29 | class JsonObject;
|
---|
| 30 |
|
---|
| 31 | // A variant that can be a any value serializable to a JSON value.
|
---|
| 32 | //
|
---|
| 33 | // It can be set to:
|
---|
| 34 | // - a boolean
|
---|
| 35 | // - a char, short, int or a long (signed or unsigned)
|
---|
| 36 | // - a string (const char*)
|
---|
| 37 | // - a reference to a JsonArray or JsonObject
|
---|
| 38 | class JsonVariant : public JsonVariantBase<JsonVariant> {
|
---|
| 39 | public:
|
---|
| 40 | template <typename T>
|
---|
| 41 | struct IsConstructibleFrom;
|
---|
| 42 |
|
---|
| 43 | // Creates an uninitialized JsonVariant
|
---|
| 44 | FORCE_INLINE JsonVariant() : _type(Internals::JSON_UNDEFINED) {}
|
---|
| 45 |
|
---|
| 46 | // Create a JsonVariant containing a boolean value.
|
---|
| 47 | // It will be serialized as "true" or "false" in JSON.
|
---|
| 48 | FORCE_INLINE JsonVariant(bool value);
|
---|
| 49 |
|
---|
| 50 | // Create a JsonVariant containing a floating point value.
|
---|
| 51 | // The second argument specifies the number of decimal digits to write in
|
---|
| 52 | // the JSON string.
|
---|
| 53 | // JsonVariant(double value, uint8_t decimals);
|
---|
| 54 | // JsonVariant(float value, uint8_t decimals);
|
---|
| 55 | template <typename T>
|
---|
| 56 | FORCE_INLINE JsonVariant(
|
---|
| 57 | T value, uint8_t decimals = 2,
|
---|
| 58 | typename TypeTraits::EnableIf<TypeTraits::IsFloatingPoint<T>::value>::type
|
---|
| 59 | * = 0) {
|
---|
| 60 | using namespace Internals;
|
---|
| 61 | _type = static_cast<JsonVariantType>(JSON_FLOAT_0_DECIMALS + decimals);
|
---|
| 62 | _content.asFloat = static_cast<JsonFloat>(value);
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | // Create a JsonVariant containing an integer value.
|
---|
| 66 | // JsonVariant(short)
|
---|
| 67 | // JsonVariant(int)
|
---|
| 68 | // JsonVariant(long)
|
---|
| 69 | template <typename T>
|
---|
| 70 | FORCE_INLINE JsonVariant(
|
---|
| 71 | T value,
|
---|
| 72 | typename TypeTraits::EnableIf<TypeTraits::IsIntegral<T>::value>::type * =
|
---|
| 73 | 0) {
|
---|
| 74 | using namespace Internals;
|
---|
| 75 | _type = JSON_INTEGER;
|
---|
| 76 | _content.asInteger = static_cast<JsonInteger>(value);
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | // Create a JsonVariant containing a string.
|
---|
| 80 | FORCE_INLINE JsonVariant(const char *value);
|
---|
| 81 |
|
---|
| 82 | // Create a JsonVariant containing an unparsed string
|
---|
| 83 | FORCE_INLINE JsonVariant(Internals::Unparsed value);
|
---|
| 84 |
|
---|
| 85 | // Create a JsonVariant containing a reference to an array.
|
---|
| 86 | FORCE_INLINE JsonVariant(JsonArray &array);
|
---|
| 87 |
|
---|
| 88 | // Create a JsonVariant containing a reference to an object.
|
---|
| 89 | FORCE_INLINE JsonVariant(JsonObject &object);
|
---|
| 90 |
|
---|
| 91 | // Get the variant as the specified type.
|
---|
| 92 | // short as<short>() const;
|
---|
| 93 | // int as<int>() const;
|
---|
| 94 | // long as<long>() const;
|
---|
| 95 | template <typename T>
|
---|
| 96 | const typename TypeTraits::EnableIf<TypeTraits::IsIntegral<T>::value, T>::type
|
---|
| 97 | as() const {
|
---|
| 98 | return static_cast<T>(asInteger());
|
---|
| 99 | }
|
---|
| 100 | // double as<double>() const;
|
---|
| 101 | // float as<float>() const;
|
---|
| 102 | template <typename T>
|
---|
| 103 | const typename TypeTraits::EnableIf<TypeTraits::IsFloatingPoint<T>::value,
|
---|
| 104 | T>::type
|
---|
| 105 | as() const {
|
---|
| 106 | return static_cast<T>(asFloat());
|
---|
| 107 | }
|
---|
| 108 | // const String as<String>() const;
|
---|
| 109 | template <typename T>
|
---|
| 110 | const typename TypeTraits::EnableIf<TypeTraits::IsSame<T, String>::value,
|
---|
| 111 | T>::type
|
---|
| 112 | as() const {
|
---|
| 113 | return toString();
|
---|
| 114 | }
|
---|
| 115 | // const char* as<const char*>() const;
|
---|
| 116 | // const char* as<char*>() const;
|
---|
| 117 | template <typename T>
|
---|
| 118 | typename TypeTraits::EnableIf<TypeTraits::IsSame<T, const char *>::value,
|
---|
| 119 | const char *>::type
|
---|
| 120 | as() const {
|
---|
| 121 | return asString();
|
---|
| 122 | }
|
---|
| 123 | // const bool as<bool>() const
|
---|
| 124 | template <typename T>
|
---|
| 125 | const typename TypeTraits::EnableIf<TypeTraits::IsSame<T, bool>::value,
|
---|
| 126 | T>::type
|
---|
| 127 | as() const {
|
---|
| 128 | return asInteger() != 0;
|
---|
| 129 | }
|
---|
| 130 | // JsonArray& as<JsonArray> const;
|
---|
| 131 | // JsonArray& as<JsonArray&> const;
|
---|
| 132 | // JsonArray& as<const JsonArray&> const;
|
---|
| 133 | template <typename T>
|
---|
| 134 | typename TypeTraits::EnableIf<
|
---|
| 135 | TypeTraits::IsSame<
|
---|
| 136 | typename TypeTraits::RemoveConst<
|
---|
| 137 | typename TypeTraits::RemoveReference<T>::type>::type,
|
---|
| 138 | JsonArray>::value,
|
---|
| 139 | JsonArray &>::type
|
---|
| 140 | as() const {
|
---|
| 141 | return asArray();
|
---|
| 142 | }
|
---|
| 143 | // JsonObject& as<JsonObject> const;
|
---|
| 144 | // JsonObject& as<JsonObject&> const;
|
---|
| 145 | // JsonObject& as<const JsonObject&> const;
|
---|
| 146 | template <typename T>
|
---|
| 147 | typename TypeTraits::EnableIf<
|
---|
| 148 | TypeTraits::IsSame<
|
---|
| 149 | typename TypeTraits::RemoveConst<
|
---|
| 150 | typename TypeTraits::RemoveReference<T>::type>::type,
|
---|
| 151 | JsonObject>::value,
|
---|
| 152 | JsonObject &>::type
|
---|
| 153 | as() const {
|
---|
| 154 | return asObject();
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | // Tells weither the variant has the specified type.
|
---|
| 158 | // Returns true if the variant has type type T, false otherwise.
|
---|
| 159 | template <typename T>
|
---|
| 160 | bool is() const;
|
---|
| 161 |
|
---|
| 162 | // Serialize the variant to a JsonWriter
|
---|
| 163 | void writeTo(Internals::JsonWriter &writer) const;
|
---|
| 164 |
|
---|
| 165 | // TODO: rename
|
---|
| 166 | template <typename T>
|
---|
| 167 | static T invalid();
|
---|
| 168 |
|
---|
| 169 | const char *asString() const;
|
---|
| 170 | JsonArray &asArray() const;
|
---|
| 171 | JsonObject &asObject() const;
|
---|
| 172 |
|
---|
| 173 | private:
|
---|
| 174 | String toString() const;
|
---|
| 175 | Internals::JsonFloat asFloat() const;
|
---|
| 176 | Internals::JsonInteger asInteger() const;
|
---|
| 177 |
|
---|
| 178 | // The current type of the variant
|
---|
| 179 | Internals::JsonVariantType _type;
|
---|
| 180 |
|
---|
| 181 | // The various alternatives for the value of the variant.
|
---|
| 182 | Internals::JsonVariantContent _content;
|
---|
| 183 | };
|
---|
| 184 |
|
---|
| 185 | inline JsonVariant float_with_n_digits(float value, uint8_t digits) {
|
---|
| 186 | return JsonVariant(value, digits);
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | inline JsonVariant double_with_n_digits(double value, uint8_t digits) {
|
---|
| 190 | return JsonVariant(value, digits);
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | template <typename T>
|
---|
| 194 | struct JsonVariant::IsConstructibleFrom {
|
---|
| 195 | static const bool value =
|
---|
| 196 | TypeTraits::IsIntegral<T>::value ||
|
---|
| 197 | TypeTraits::IsFloatingPoint<T>::value ||
|
---|
| 198 | TypeTraits::IsSame<T, bool>::value ||
|
---|
| 199 | TypeTraits::IsSame<T, char *>::value ||
|
---|
| 200 | TypeTraits::IsSame<T, const char *>::value ||
|
---|
| 201 | TypeTraits::IsSame<T, JsonArray &>::value ||
|
---|
| 202 | TypeTraits::IsSame<T, const JsonArray &>::value ||
|
---|
| 203 | TypeTraits::IsSame<T, JsonArraySubscript &>::value ||
|
---|
| 204 | TypeTraits::IsSame<T, const JsonArraySubscript &>::value ||
|
---|
| 205 | TypeTraits::IsSame<T, JsonObject &>::value ||
|
---|
| 206 | TypeTraits::IsSame<T, const JsonObject &>::value ||
|
---|
| 207 | TypeTraits::IsSame<T, JsonObjectSubscript<const char *> &>::value ||
|
---|
| 208 | TypeTraits::IsSame<T, const JsonObjectSubscript<const char *> &>::value ||
|
---|
| 209 | TypeTraits::IsSame<T, JsonObjectSubscript<String> &>::value ||
|
---|
| 210 | TypeTraits::IsSame<T, const JsonObjectSubscript<String> &>::value ||
|
---|
| 211 | TypeTraits::IsSame<T, JsonVariant &>::value ||
|
---|
| 212 | TypeTraits::IsSame<T, const JsonVariant &>::value;
|
---|
| 213 | };
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 | // Include inline implementations
|
---|
| 217 | #include "JsonVariant.ipp"
|
---|