mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-18 21:12:25 +02:00
Implemented JsonVariant comparisons with template friends
This commit is contained in:
@ -10,7 +10,6 @@
|
|||||||
#include "ArduinoJson/DynamicJsonBuffer.hpp"
|
#include "ArduinoJson/DynamicJsonBuffer.hpp"
|
||||||
#include "ArduinoJson/JsonArray.hpp"
|
#include "ArduinoJson/JsonArray.hpp"
|
||||||
#include "ArduinoJson/JsonObject.hpp"
|
#include "ArduinoJson/JsonObject.hpp"
|
||||||
#include "ArduinoJson/JsonVariantComparisons.hpp"
|
|
||||||
#include "ArduinoJson/StaticJsonBuffer.hpp"
|
#include "ArduinoJson/StaticJsonBuffer.hpp"
|
||||||
|
|
||||||
#include "ArduinoJson/Deserialization/JsonParserImpl.hpp"
|
#include "ArduinoJson/Deserialization/JsonParserImpl.hpp"
|
||||||
|
@ -1,69 +0,0 @@
|
|||||||
// Copyright Benoit Blanchon 2014-2017
|
|
||||||
// MIT License
|
|
||||||
//
|
|
||||||
// Arduino JSON library
|
|
||||||
// https://bblanchon.github.io/ArduinoJson/
|
|
||||||
// If you like this project, please add a star!
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include "../JsonVariantBase.hpp"
|
|
||||||
#include "../StringTraits/StringTraits.hpp"
|
|
||||||
#include "../TypeTraits/EnableIf.hpp"
|
|
||||||
|
|
||||||
namespace ArduinoJson {
|
|
||||||
namespace Internals {
|
|
||||||
template <typename TComparand, typename Enable = void>
|
|
||||||
struct JsonVariantComparer {};
|
|
||||||
|
|
||||||
template <typename TString>
|
|
||||||
struct JsonVariantComparer<
|
|
||||||
TString,
|
|
||||||
typename TypeTraits::EnableIf<TypeTraits::IsString<TString>::value>::type> {
|
|
||||||
template <typename TVariant>
|
|
||||||
static bool equals(const JsonVariantBase<TVariant> &variant,
|
|
||||||
const TString &comparand) {
|
|
||||||
const char *value = variant.template as<const char *>();
|
|
||||||
return Internals::StringTraits<TString>::equals(comparand, value);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename TComparand>
|
|
||||||
struct JsonVariantComparer<
|
|
||||||
TComparand, typename TypeTraits::EnableIf<
|
|
||||||
!TypeTraits::IsVariant<TComparand>::value &&
|
|
||||||
!TypeTraits::IsString<TComparand>::value>::type> {
|
|
||||||
template <typename TVariant>
|
|
||||||
static bool equals(const JsonVariantBase<TVariant> &variant,
|
|
||||||
const TComparand &comparand) {
|
|
||||||
return variant.template as<TComparand>() == comparand;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename TVariant2>
|
|
||||||
struct JsonVariantComparer<TVariant2,
|
|
||||||
typename TypeTraits::EnableIf<
|
|
||||||
TypeTraits::IsVariant<TVariant2>::value>::type> {
|
|
||||||
template <typename TVariant1>
|
|
||||||
static bool equals(const JsonVariantBase<TVariant1> &left,
|
|
||||||
const TVariant2 &right) {
|
|
||||||
if (left.template is<bool>() && right.template is<bool>())
|
|
||||||
return left.template as<bool>() == right.template as<bool>();
|
|
||||||
if (left.template is<JsonInteger>() && right.template is<JsonInteger>())
|
|
||||||
return left.template as<JsonInteger>() ==
|
|
||||||
right.template as<JsonInteger>();
|
|
||||||
if (left.template is<JsonFloat>() && right.template is<JsonFloat>())
|
|
||||||
return left.template as<JsonFloat>() == right.template as<JsonFloat>();
|
|
||||||
if (left.template is<JsonArray>() && right.template is<JsonArray>())
|
|
||||||
return left.template as<JsonArray>() == right.template as<JsonArray>();
|
|
||||||
if (left.template is<JsonObject>() && right.template is<JsonObject>())
|
|
||||||
return left.template as<JsonObject>() == right.template as<JsonObject>();
|
|
||||||
if (left.template is<char *>() && right.template is<char *>())
|
|
||||||
return strcmp(left.template as<char *>(), right.template as<char *>()) ==
|
|
||||||
0;
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
@ -106,15 +106,16 @@ inline const JsonArraySubscript JsonArray::operator[](size_t index) const {
|
|||||||
return JsonArraySubscript(*const_cast<JsonArray*>(this), index);
|
return JsonArraySubscript(*const_cast<JsonArray*>(this), index);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename TImplem>
|
template <typename TImpl>
|
||||||
inline JsonArraySubscript JsonVariantBase<TImplem>::operator[](size_t index) {
|
inline JsonArraySubscript JsonVariantSubscripts<TImpl>::operator[](
|
||||||
return as<JsonArray>()[index];
|
size_t index) {
|
||||||
|
return impl()->template as<JsonArray>()[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename TImplem>
|
template <typename TImpl>
|
||||||
inline const JsonArraySubscript JsonVariantBase<TImplem>::operator[](
|
inline const JsonArraySubscript JsonVariantSubscripts<TImpl>::operator[](
|
||||||
size_t index) const {
|
size_t index) const {
|
||||||
return as<JsonArray>()[index];
|
return impl()->template as<JsonArray>()[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace ArduinoJson
|
} // namespace ArduinoJson
|
||||||
|
@ -7,133 +7,17 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "Data/JsonVariantAs.hpp"
|
#include "JsonVariantCasts.hpp"
|
||||||
#include "Polyfills/attributes.hpp"
|
#include "JsonVariantComparisons.hpp"
|
||||||
|
#include "JsonVariantSubscripts.hpp"
|
||||||
#include "Serialization/JsonPrintable.hpp"
|
#include "Serialization/JsonPrintable.hpp"
|
||||||
|
|
||||||
namespace ArduinoJson {
|
namespace ArduinoJson {
|
||||||
|
|
||||||
// Forward declarations.
|
|
||||||
class JsonArraySubscript;
|
|
||||||
template <typename TKey>
|
|
||||||
class JsonObjectSubscript;
|
|
||||||
|
|
||||||
template <typename TImpl>
|
template <typename TImpl>
|
||||||
class JsonVariantBase : public Internals::JsonPrintable<TImpl> {
|
class JsonVariantBase : public Internals::JsonPrintable<TImpl>,
|
||||||
public:
|
public JsonVariantCasts<TImpl>,
|
||||||
#if ARDUINOJSON_ENABLE_DEPRECATED
|
public JsonVariantComparisons<TImpl>,
|
||||||
DEPRECATED("use as<JsonArray>() instead")
|
public JsonVariantSubscripts<TImpl>,
|
||||||
FORCE_INLINE JsonArray &asArray() const {
|
public TypeTraits::JsonVariantTag {};
|
||||||
return as<JsonArray>();
|
|
||||||
}
|
|
||||||
|
|
||||||
DEPRECATED("use as<JsonObject>() instead")
|
|
||||||
FORCE_INLINE JsonObject &asObject() const {
|
|
||||||
return as<JsonObject>();
|
|
||||||
}
|
|
||||||
|
|
||||||
DEPRECATED("use as<char*>() instead")
|
|
||||||
FORCE_INLINE const char *asString() const {
|
|
||||||
return as<const char *>();
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// 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<JsonArray &>();
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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<JsonObject &>();
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
FORCE_INLINE operator T() const {
|
|
||||||
return as<T>();
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
FORCE_INLINE const typename Internals::JsonVariantAs<T>::type as() const {
|
|
||||||
return impl()->template as<T>();
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
FORCE_INLINE bool is() const {
|
|
||||||
return impl()->template is<T>();
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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 as<JsonArray>().size() + as<JsonObject>().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[](size_t index) const;
|
|
||||||
FORCE_INLINE JsonArraySubscript operator[](size_t index);
|
|
||||||
|
|
||||||
// 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.
|
|
||||||
//
|
|
||||||
// const JsonObjectSubscript operator[](TKey) const;
|
|
||||||
// TKey = const std::string&, const String&
|
|
||||||
template <typename TString>
|
|
||||||
FORCE_INLINE typename TypeTraits::EnableIf<
|
|
||||||
Internals::StringTraits<TString>::has_equals,
|
|
||||||
const JsonObjectSubscript<const TString &> >::type
|
|
||||||
operator[](const TString &key) const {
|
|
||||||
return as<JsonObject>()[key];
|
|
||||||
}
|
|
||||||
//
|
|
||||||
// const JsonObjectSubscript operator[](TKey) const;
|
|
||||||
// TKey = const std::string&, const String&
|
|
||||||
template <typename TString>
|
|
||||||
FORCE_INLINE typename TypeTraits::EnableIf<
|
|
||||||
Internals::StringTraits<TString>::has_equals,
|
|
||||||
JsonObjectSubscript<const TString &> >::type
|
|
||||||
operator[](const TString &key) {
|
|
||||||
return as<JsonObject>()[key];
|
|
||||||
}
|
|
||||||
//
|
|
||||||
// JsonObjectSubscript operator[](TKey);
|
|
||||||
// TKey = const char*, const char[N], const FlashStringHelper*
|
|
||||||
template <typename TString>
|
|
||||||
FORCE_INLINE typename TypeTraits::EnableIf<
|
|
||||||
Internals::StringTraits<const TString *>::has_equals,
|
|
||||||
JsonObjectSubscript<const TString *> >::type
|
|
||||||
operator[](const TString *key) {
|
|
||||||
return as<JsonObject>()[key];
|
|
||||||
}
|
|
||||||
//
|
|
||||||
// JsonObjectSubscript operator[](TKey);
|
|
||||||
// TKey = const char*, const char[N], const FlashStringHelper*
|
|
||||||
template <typename TString>
|
|
||||||
FORCE_INLINE typename TypeTraits::EnableIf<
|
|
||||||
Internals::StringTraits<TString *>::has_equals,
|
|
||||||
const JsonObjectSubscript<const TString *> >::type
|
|
||||||
operator[](const TString *key) const {
|
|
||||||
return as<JsonObject>()[key];
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
const TImpl *impl() const {
|
|
||||||
return static_cast<const TImpl *>(this);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
namespace TypeTraits {
|
|
||||||
template <typename T>
|
|
||||||
struct IsVariant : IsBaseOf<JsonVariantBase<T>, T> {};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
60
src/ArduinoJson/JsonVariantCasts.hpp
Normal file
60
src/ArduinoJson/JsonVariantCasts.hpp
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
// Copyright Benoit Blanchon 2014-2017
|
||||||
|
// MIT License
|
||||||
|
//
|
||||||
|
// Arduino JSON library
|
||||||
|
// https://bblanchon.github.io/ArduinoJson/
|
||||||
|
// If you like this project, please add a star!
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "Data/JsonVariantAs.hpp"
|
||||||
|
#include "Polyfills/attributes.hpp"
|
||||||
|
|
||||||
|
namespace ArduinoJson {
|
||||||
|
|
||||||
|
template <typename TImpl>
|
||||||
|
class JsonVariantCasts {
|
||||||
|
public:
|
||||||
|
#if ARDUINOJSON_ENABLE_DEPRECATED
|
||||||
|
DEPRECATED("use as<JsonArray>() instead")
|
||||||
|
FORCE_INLINE JsonArray &asArray() const {
|
||||||
|
return impl()->template as<JsonArray>();
|
||||||
|
}
|
||||||
|
|
||||||
|
DEPRECATED("use as<JsonObject>() instead")
|
||||||
|
FORCE_INLINE JsonObject &asObject() const {
|
||||||
|
return impl()->template as<JsonObject>();
|
||||||
|
}
|
||||||
|
|
||||||
|
DEPRECATED("use as<char*>() instead")
|
||||||
|
FORCE_INLINE const char *asString() const {
|
||||||
|
return impl()->template as<const char *>();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// 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 impl()->template as<JsonArray &>();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 impl()->template as<JsonObject &>();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
FORCE_INLINE operator T() const {
|
||||||
|
return impl()->template as<T>();
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
const TImpl *impl() const {
|
||||||
|
return static_cast<const TImpl *>(this);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
@ -7,82 +7,138 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "Data/JsonVariantComparer.hpp"
|
#include "StringTraits/StringTraits.hpp"
|
||||||
|
#include "TypeTraits/EnableIf.hpp"
|
||||||
|
#include "TypeTraits/IsVariant.hpp"
|
||||||
|
|
||||||
namespace ArduinoJson {
|
namespace ArduinoJson {
|
||||||
template <typename TVariant, typename TComparand>
|
|
||||||
inline bool operator==(const JsonVariantBase<TVariant> &variant,
|
template <typename TImpl>
|
||||||
|
class JsonVariantComparisons {
|
||||||
|
public:
|
||||||
|
template <typename TComparand>
|
||||||
|
friend bool operator==(const JsonVariantComparisons &variant,
|
||||||
TComparand comparand) {
|
TComparand comparand) {
|
||||||
return Internals::JsonVariantComparer<TComparand>::equals(variant, comparand);
|
return variant.equals(comparand);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename TVariant, typename TComparand>
|
template <typename TComparand>
|
||||||
inline typename TypeTraits::EnableIf<!TypeTraits::IsVariant<TComparand>::value,
|
friend
|
||||||
|
typename TypeTraits::EnableIf<!TypeTraits::IsVariant<TComparand>::value,
|
||||||
bool>::type
|
bool>::type
|
||||||
operator==(TComparand comparand, const JsonVariantBase<TVariant> &variant) {
|
operator==(TComparand comparand, const JsonVariantComparisons &variant) {
|
||||||
return Internals::JsonVariantComparer<TComparand>::equals(variant, comparand);
|
return variant.equals(comparand);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename TVariant, typename TComparand>
|
template <typename TComparand>
|
||||||
inline bool operator!=(const JsonVariantBase<TVariant> &variant,
|
friend bool operator!=(const JsonVariantComparisons &variant,
|
||||||
TComparand comparand) {
|
TComparand comparand) {
|
||||||
return !Internals::JsonVariantComparer<TComparand>::equals(variant,
|
return !variant.equals(comparand);
|
||||||
comparand);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename TVariant, typename TComparand>
|
template <typename TComparand>
|
||||||
inline typename TypeTraits::EnableIf<!TypeTraits::IsVariant<TComparand>::value,
|
friend
|
||||||
|
typename TypeTraits::EnableIf<!TypeTraits::IsVariant<TComparand>::value,
|
||||||
bool>::type
|
bool>::type
|
||||||
operator!=(TComparand comparand, const JsonVariantBase<TVariant> &variant) {
|
operator!=(TComparand comparand, const JsonVariantComparisons &variant) {
|
||||||
return !Internals::JsonVariantComparer<TComparand>::equals(variant,
|
return !variant.equals(comparand);
|
||||||
comparand);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename TVariant, typename TComparand>
|
template <typename TComparand>
|
||||||
inline bool operator<=(const JsonVariantBase<TVariant> &left,
|
friend bool operator<=(const JsonVariantComparisons &left, TComparand right) {
|
||||||
TComparand right) {
|
return left.as<TComparand>() <= right;
|
||||||
return left.template as<TComparand>() <= right;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename TVariant, typename TComparand>
|
template <typename TComparand>
|
||||||
inline bool operator<=(TComparand comparand,
|
friend bool operator<=(TComparand comparand,
|
||||||
const JsonVariantBase<TVariant> &variant) {
|
const JsonVariantComparisons &variant) {
|
||||||
return comparand <= variant.template as<TComparand>();
|
return comparand <= variant.as<TComparand>();
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename TVariant, typename TComparand>
|
template <typename TComparand>
|
||||||
inline bool operator>=(const JsonVariantBase<TVariant> &variant,
|
friend bool operator>=(const JsonVariantComparisons &variant,
|
||||||
TComparand comparand) {
|
TComparand comparand) {
|
||||||
return variant.template as<TComparand>() >= comparand;
|
return variant.as<TComparand>() >= comparand;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename TVariant, typename TComparand>
|
template <typename TComparand>
|
||||||
inline bool operator>=(TComparand comparand,
|
friend bool operator>=(TComparand comparand,
|
||||||
const JsonVariantBase<TVariant> &variant) {
|
const JsonVariantComparisons &variant) {
|
||||||
return comparand >= variant.template as<TComparand>();
|
return comparand >= variant.as<TComparand>();
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename TVariant, typename TComparand>
|
template <typename TComparand>
|
||||||
inline bool operator<(const JsonVariantBase<TVariant> &varian,
|
friend bool operator<(const JsonVariantComparisons &varian,
|
||||||
TComparand comparand) {
|
TComparand comparand) {
|
||||||
return varian.template as<TComparand>() < comparand;
|
return varian.as<TComparand>() < comparand;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename TVariant, typename TComparand>
|
template <typename TComparand>
|
||||||
inline bool operator<(TComparand comparand,
|
friend bool operator<(TComparand comparand,
|
||||||
const JsonVariantBase<TVariant> &variant) {
|
const JsonVariantComparisons &variant) {
|
||||||
return comparand < variant.template as<TComparand>();
|
return comparand < variant.as<TComparand>();
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename TVariant, typename TComparand>
|
template <typename TComparand>
|
||||||
inline bool operator>(const JsonVariantBase<TVariant> &variant,
|
friend bool operator>(const JsonVariantComparisons &variant,
|
||||||
TComparand comparand) {
|
TComparand comparand) {
|
||||||
return variant.template as<TComparand>() > comparand;
|
return variant.as<TComparand>() > comparand;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename TVariant, typename TComparand>
|
template <typename TComparand>
|
||||||
inline bool operator>(TComparand comparand,
|
friend bool operator>(TComparand comparand,
|
||||||
const JsonVariantBase<TVariant> &variant) {
|
const JsonVariantComparisons &variant) {
|
||||||
return comparand > variant.template as<TComparand>();
|
return comparand > variant.as<TComparand>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
const TImpl *impl() const {
|
||||||
|
return static_cast<const TImpl *>(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
const typename Internals::JsonVariantAs<T>::type as() const {
|
||||||
|
return impl()->template as<T>();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
bool is() const {
|
||||||
|
return impl()->template is<T>();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename TString>
|
||||||
|
typename TypeTraits::EnableIf<TypeTraits::IsString<TString>::value,
|
||||||
|
bool>::type
|
||||||
|
equals(const TString &comparand) const {
|
||||||
|
const char *value = as<const char *>();
|
||||||
|
return Internals::StringTraits<TString>::equals(comparand, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename TComparand>
|
||||||
|
typename TypeTraits::EnableIf<!TypeTraits::IsVariant<TComparand>::value &&
|
||||||
|
!TypeTraits::IsString<TComparand>::value,
|
||||||
|
bool>::type
|
||||||
|
equals(const TComparand &comparand) const {
|
||||||
|
return as<TComparand>() == comparand;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename TVariant2>
|
||||||
|
bool equals(const JsonVariantComparisons<TVariant2> &right) const {
|
||||||
|
using namespace Internals;
|
||||||
|
if (is<bool>() && right.template is<bool>())
|
||||||
|
return as<bool>() == right.template as<bool>();
|
||||||
|
if (is<JsonInteger>() && right.template is<JsonInteger>())
|
||||||
|
return as<JsonInteger>() == right.template as<JsonInteger>();
|
||||||
|
if (is<JsonFloat>() && right.template is<JsonFloat>())
|
||||||
|
return as<JsonFloat>() == right.template as<JsonFloat>();
|
||||||
|
if (is<JsonArray>() && right.template is<JsonArray>())
|
||||||
|
return as<JsonArray>() == right.template as<JsonArray>();
|
||||||
|
if (is<JsonObject>() && right.template is<JsonObject>())
|
||||||
|
return as<JsonObject>() == right.template as<JsonObject>();
|
||||||
|
if (is<char *>() && right.template is<char *>())
|
||||||
|
return strcmp(as<char *>(), right.template as<char *>()) == 0;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
89
src/ArduinoJson/JsonVariantSubscripts.hpp
Normal file
89
src/ArduinoJson/JsonVariantSubscripts.hpp
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
// Copyright Benoit Blanchon 2014-2017
|
||||||
|
// MIT License
|
||||||
|
//
|
||||||
|
// Arduino JSON library
|
||||||
|
// https://bblanchon.github.io/ArduinoJson/
|
||||||
|
// If you like this project, please add a star!
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "Data/JsonVariantAs.hpp"
|
||||||
|
#include "Polyfills/attributes.hpp"
|
||||||
|
#include "StringTraits/StringTraits.hpp"
|
||||||
|
#include "TypeTraits/EnableIf.hpp"
|
||||||
|
|
||||||
|
namespace ArduinoJson {
|
||||||
|
|
||||||
|
// Forward declarations.
|
||||||
|
class JsonArraySubscript;
|
||||||
|
template <typename TKey>
|
||||||
|
class JsonObjectSubscript;
|
||||||
|
|
||||||
|
template <typename TImpl>
|
||||||
|
class JsonVariantSubscripts {
|
||||||
|
public:
|
||||||
|
// 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 impl()->template as<JsonArray>().size() +
|
||||||
|
impl()->template as<JsonObject>().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[](size_t index) const;
|
||||||
|
FORCE_INLINE JsonArraySubscript operator[](size_t index);
|
||||||
|
|
||||||
|
// 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.
|
||||||
|
//
|
||||||
|
// const JsonObjectSubscript operator[](TKey) const;
|
||||||
|
// TKey = const std::string&, const String&
|
||||||
|
template <typename TString>
|
||||||
|
FORCE_INLINE typename TypeTraits::EnableIf<
|
||||||
|
Internals::StringTraits<TString>::has_equals,
|
||||||
|
const JsonObjectSubscript<const TString &> >::type
|
||||||
|
operator[](const TString &key) const {
|
||||||
|
return impl()->template as<JsonObject>()[key];
|
||||||
|
}
|
||||||
|
//
|
||||||
|
// const JsonObjectSubscript operator[](TKey) const;
|
||||||
|
// TKey = const std::string&, const String&
|
||||||
|
template <typename TString>
|
||||||
|
FORCE_INLINE typename TypeTraits::EnableIf<
|
||||||
|
Internals::StringTraits<TString>::has_equals,
|
||||||
|
JsonObjectSubscript<const TString &> >::type
|
||||||
|
operator[](const TString &key) {
|
||||||
|
return impl()->template as<JsonObject>()[key];
|
||||||
|
}
|
||||||
|
//
|
||||||
|
// JsonObjectSubscript operator[](TKey);
|
||||||
|
// TKey = const char*, const char[N], const FlashStringHelper*
|
||||||
|
template <typename TString>
|
||||||
|
FORCE_INLINE typename TypeTraits::EnableIf<
|
||||||
|
Internals::StringTraits<const TString *>::has_equals,
|
||||||
|
JsonObjectSubscript<const TString *> >::type
|
||||||
|
operator[](const TString *key) {
|
||||||
|
return impl()->template as<JsonObject>()[key];
|
||||||
|
}
|
||||||
|
//
|
||||||
|
// JsonObjectSubscript operator[](TKey);
|
||||||
|
// TKey = const char*, const char[N], const FlashStringHelper*
|
||||||
|
template <typename TString>
|
||||||
|
FORCE_INLINE typename TypeTraits::EnableIf<
|
||||||
|
Internals::StringTraits<TString *>::has_equals,
|
||||||
|
const JsonObjectSubscript<const TString *> >::type
|
||||||
|
operator[](const TString *key) const {
|
||||||
|
return impl()->template as<JsonObject>()[key];
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
const TImpl *impl() const {
|
||||||
|
return static_cast<const TImpl *>(this);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
20
src/ArduinoJson/TypeTraits/IsVariant.hpp
Normal file
20
src/ArduinoJson/TypeTraits/IsVariant.hpp
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
// Copyright Benoit Blanchon 2014-2017
|
||||||
|
// MIT License
|
||||||
|
//
|
||||||
|
// Arduino JSON library
|
||||||
|
// https://bblanchon.github.io/ArduinoJson/
|
||||||
|
// If you like this project, please add a star!
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "IsBaseOf.hpp"
|
||||||
|
|
||||||
|
namespace ArduinoJson {
|
||||||
|
namespace TypeTraits {
|
||||||
|
|
||||||
|
class JsonVariantTag {};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct IsVariant : IsBaseOf<JsonVariantTag, T> {};
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user