// Copyright Benoit Blanchon 2014-2016 // MIT License // // Arduino JSON library // https://github.com/bblanchon/ArduinoJson // If you like this project, please add a star! #pragma once #include "Internals/ForceInline.hpp" #include "JsonObjectKey.hpp" namespace ArduinoJson { // Forward declarations. class JsonArraySubscript; template class JsonObjectSubscript; template class JsonVariantBase : public Internals::JsonPrintable { public: FORCE_INLINE const char *asString() const { return as(); } // Gets the variant as an array. // Returns a reference to the JsonArray or JsonArray::invalid() if the // variant // is not an array. FORCE_INLINE operator JsonArray &() const { return as(); } FORCE_INLINE JsonArray &asArray() const { return as(); } // Gets the variant as an object. // Returns a reference to the JsonObject or JsonObject::invalid() if the // variant is not an object. FORCE_INLINE operator JsonObject &() const { return as(); } FORCE_INLINE JsonObject &asObject() const { return as(); } template FORCE_INLINE operator T() const { return as(); } template FORCE_INLINE const T as() const { return impl()->template as(); } // Mimics an array or an object. // Returns the size of the array or object if the variant has that type. // Returns 0 if the variant is neither an array nor an object size_t size() const { return asArray().size() + asObject().size(); } // Mimics an array. // Returns the element at specified index if the variant is an array. // Returns JsonVariant::invalid() if the variant is not an array. FORCE_INLINE const JsonArraySubscript operator[](int index) const; // Mimics an object. // Returns the value associated with the specified key if the variant is // an object. // Return JsonVariant::invalid() if the variant is not an object. FORCE_INLINE const JsonObjectSubscript operator[]( const char *key) const; FORCE_INLINE const JsonObjectSubscript operator[]( const String &key) const; // Serialize the variant to a JsonWriter void writeTo(Internals::JsonWriter &writer) const; private: const TImpl *impl() const { return static_cast(this); } }; template inline bool operator==(const JsonVariantBase &left, TComparand right) { return left.template as() == right; } template inline bool operator==(TComparand left, const JsonVariantBase &right) { return left == right.template as(); } template inline bool operator!=(const JsonVariantBase &left, TComparand right) { return left.template as() != right; } template inline bool operator!=(TComparand left, const JsonVariantBase &right) { return left != right.template as(); } template inline bool operator<=(const JsonVariantBase &left, TComparand right) { return left.template as() <= right; } template inline bool operator<=(TComparand left, const JsonVariantBase &right) { return left <= right.template as(); } template inline bool operator>=(const JsonVariantBase &left, TComparand right) { return left.template as() >= right; } template inline bool operator>=(TComparand left, const JsonVariantBase &right) { return left >= right.template as(); } template inline bool operator<(const JsonVariantBase &left, TComparand right) { return left.template as() < right; } template inline bool operator<(TComparand left, const JsonVariantBase &right) { return left < right.template as(); } template inline bool operator>(const JsonVariantBase &left, TComparand right) { return left.template as() > right; } template inline bool operator>(TComparand left, const JsonVariantBase &right) { return left > right.template as(); } }