// ArduinoJson - https://arduinojson.org // Copyright Benoit Blanchon 2014-2021 // MIT License #pragma once #include "lib/ArduinoJson/Array/ArrayRef.hpp" #include "lib/ArduinoJson/Configuration.hpp" #include "lib/ArduinoJson/Numbers/convertNumber.hpp" #include "lib/ArduinoJson/Numbers/parseNumber.hpp" #include "lib/ArduinoJson/Object/ObjectRef.hpp" #include "lib/ArduinoJson/Variant/VariantRef.hpp" #include // for strcmp namespace ARDUINOJSON_NAMESPACE { template inline T VariantData::asIntegral() const { switch (type()) { case VALUE_IS_BOOLEAN: return _content.asBoolean; case VALUE_IS_UNSIGNED_INTEGER: return convertNumber(_content.asUnsignedInteger); case VALUE_IS_SIGNED_INTEGER: return convertNumber(_content.asSignedInteger); case VALUE_IS_LINKED_STRING: case VALUE_IS_OWNED_STRING: return parseNumber(_content.asString); case VALUE_IS_FLOAT: return convertNumber(_content.asFloat); default: return 0; } } inline bool VariantData::asBoolean() const { switch (type()) { case VALUE_IS_BOOLEAN: return _content.asBoolean; case VALUE_IS_SIGNED_INTEGER: case VALUE_IS_UNSIGNED_INTEGER: return _content.asUnsignedInteger != 0; case VALUE_IS_FLOAT: return _content.asFloat != 0; case VALUE_IS_NULL: return false; default: return true; } } // T = float/double template inline T VariantData::asFloat() const { switch (type()) { case VALUE_IS_BOOLEAN: return static_cast(_content.asBoolean); case VALUE_IS_UNSIGNED_INTEGER: return static_cast(_content.asUnsignedInteger); case VALUE_IS_SIGNED_INTEGER: return static_cast(_content.asSignedInteger); case VALUE_IS_LINKED_STRING: case VALUE_IS_OWNED_STRING: return parseNumber(_content.asString); case VALUE_IS_FLOAT: return static_cast(_content.asFloat); default: return 0; } } inline const char *VariantData::asString() const { switch (type()) { case VALUE_IS_LINKED_STRING: case VALUE_IS_OWNED_STRING: return _content.asString; default: return 0; } } template inline typename enable_if::value, ArrayRef>::type VariantRef::to() const { return ArrayRef(_pool, variantToArray(_data)); } template typename enable_if::value, ObjectRef>::type VariantRef::to() const { return ObjectRef(_pool, variantToObject(_data)); } template typename enable_if::value, VariantRef>::type VariantRef::to() const { variantSetNull(_data); return *this; } inline VariantConstRef VariantConstRef::getElement(size_t index) const { return ArrayConstRef(_data != 0 ? _data->asArray() : 0)[index]; } inline VariantRef VariantRef::addElement() const { return VariantRef(_pool, variantAddElement(_data, _pool)); } inline VariantRef VariantRef::getElement(size_t index) const { return VariantRef(_pool, _data != 0 ? _data->getElement(index) : 0); } inline VariantRef VariantRef::getOrAddElement(size_t index) const { return VariantRef(_pool, variantGetOrAddElement(_data, index, _pool)); } template inline VariantRef VariantRef::getMember(TChar *key) const { return VariantRef(_pool, _data != 0 ? _data->getMember(adaptString(key)) : 0); } template inline typename enable_if::value, VariantRef>::type VariantRef::getMember(const TString &key) const { return VariantRef(_pool, _data != 0 ? _data->getMember(adaptString(key)) : 0); } template inline VariantRef VariantRef::getOrAddMember(TChar *key) const { return VariantRef(_pool, variantGetOrAddMember(_data, key, _pool)); } template inline VariantRef VariantRef::getOrAddMember(const TString &key) const { return VariantRef(_pool, variantGetOrAddMember(_data, key, _pool)); } inline VariantConstRef operator|(VariantConstRef preferedValue, VariantConstRef defaultValue) { return preferedValue ? preferedValue : defaultValue; } } // namespace ARDUINOJSON_NAMESPACE