forked from bblanchon/ArduinoJson
Added operator ==
to compare JsonVariant
and strings (issue #402)
This commit is contained in:
@ -1,6 +1,11 @@
|
|||||||
ArduinoJson: change log
|
ArduinoJson: change log
|
||||||
=======================
|
=======================
|
||||||
|
|
||||||
|
HEAD
|
||||||
|
----
|
||||||
|
|
||||||
|
* Added operator `==` to compare `JsonVariant` and strings (issue #402)
|
||||||
|
|
||||||
v5.7.3
|
v5.7.3
|
||||||
------
|
------
|
||||||
|
|
||||||
|
@ -39,9 +39,14 @@ void setup() {
|
|||||||
// JsonBuffer.
|
// JsonBuffer.
|
||||||
root["sensor"] = F("gps");
|
root["sensor"] = F("gps");
|
||||||
|
|
||||||
|
// You can compare the content of a JsonVariant to a Flash String
|
||||||
|
if (root["sensor"] == F("gps")) {
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
#warning PROGMEM is only supported on AVR architecture
|
#warning PROGMEM is not supported on this platform
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
@ -48,6 +48,11 @@ void setup() {
|
|||||||
// WARNING: the content of the String will be duplicated in the JsonBuffer.
|
// WARNING: the content of the String will be duplicated in the JsonBuffer.
|
||||||
root[String("sen") + "sor"] = String("gp") + "s";
|
root[String("sen") + "sor"] = String("gp") + "s";
|
||||||
|
|
||||||
|
// You can compare the content of a JsonObject with a String
|
||||||
|
if (root["sensor"] == sensor) {
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
|
||||||
// Lastly, you can print the resulting JSON to a String
|
// Lastly, you can print the resulting JSON to a String
|
||||||
String output;
|
String output;
|
||||||
root.printTo(output);
|
root.printTo(output);
|
||||||
|
@ -10,6 +10,7 @@
|
|||||||
#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/Internals/JsonParserImpl.hpp"
|
#include "ArduinoJson/Internals/JsonParserImpl.hpp"
|
||||||
|
@ -97,64 +97,4 @@ class JsonVariantBase : public Internals::JsonPrintable<TImpl> {
|
|||||||
return static_cast<const TImpl *>(this);
|
return static_cast<const TImpl *>(this);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename TImpl, typename TComparand>
|
|
||||||
inline bool operator==(const JsonVariantBase<TImpl> &left, TComparand right) {
|
|
||||||
return left.template as<TComparand>() == right;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename TImpl, typename TComparand>
|
|
||||||
inline bool operator==(TComparand left, const JsonVariantBase<TImpl> &right) {
|
|
||||||
return left == right.template as<TComparand>();
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename TImpl, typename TComparand>
|
|
||||||
inline bool operator!=(const JsonVariantBase<TImpl> &left, TComparand right) {
|
|
||||||
return left.template as<TComparand>() != right;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename TImpl, typename TComparand>
|
|
||||||
inline bool operator!=(TComparand left, const JsonVariantBase<TImpl> &right) {
|
|
||||||
return left != right.template as<TComparand>();
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename TImpl, typename TComparand>
|
|
||||||
inline bool operator<=(const JsonVariantBase<TImpl> &left, TComparand right) {
|
|
||||||
return left.template as<TComparand>() <= right;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename TImpl, typename TComparand>
|
|
||||||
inline bool operator<=(TComparand left, const JsonVariantBase<TImpl> &right) {
|
|
||||||
return left <= right.template as<TComparand>();
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename TImpl, typename TComparand>
|
|
||||||
inline bool operator>=(const JsonVariantBase<TImpl> &left, TComparand right) {
|
|
||||||
return left.template as<TComparand>() >= right;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename TImpl, typename TComparand>
|
|
||||||
inline bool operator>=(TComparand left, const JsonVariantBase<TImpl> &right) {
|
|
||||||
return left >= right.template as<TComparand>();
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename TImpl, typename TComparand>
|
|
||||||
inline bool operator<(const JsonVariantBase<TImpl> &left, TComparand right) {
|
|
||||||
return left.template as<TComparand>() < right;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename TImpl, typename TComparand>
|
|
||||||
inline bool operator<(TComparand left, const JsonVariantBase<TImpl> &right) {
|
|
||||||
return left < right.template as<TComparand>();
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename TImpl, typename TComparand>
|
|
||||||
inline bool operator>(const JsonVariantBase<TImpl> &left, TComparand right) {
|
|
||||||
return left.template as<TComparand>() > right;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename TImpl, typename TComparand>
|
|
||||||
inline bool operator>(TComparand left, const JsonVariantBase<TImpl> &right) {
|
|
||||||
return left > right.template as<TComparand>();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
106
include/ArduinoJson/JsonVariantComparisons.hpp
Normal file
106
include/ArduinoJson/JsonVariantComparisons.hpp
Normal file
@ -0,0 +1,106 @@
|
|||||||
|
// 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/StringFuncs.hpp"
|
||||||
|
#include "JsonVariantBase.hpp"
|
||||||
|
#include "TypeTraits/EnableIf.hpp"
|
||||||
|
|
||||||
|
namespace ArduinoJson {
|
||||||
|
template <typename TVariant, typename TComparand, typename Enable = void>
|
||||||
|
struct JsonVariantComparer {
|
||||||
|
static bool equals(const TVariant &variant, const TComparand &comparand) {
|
||||||
|
return variant.template as<TComparand>() == comparand;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename TVariant, typename TString>
|
||||||
|
struct JsonVariantComparer<
|
||||||
|
TVariant, TString, typename TypeTraits::EnableIf<
|
||||||
|
Internals::StringFuncs<TString>::has_equals>::type> {
|
||||||
|
static bool equals(const TVariant &variant, const TString &comparand) {
|
||||||
|
const char *value = variant.template as<const char *>();
|
||||||
|
return Internals::StringFuncs<TString>::equals(comparand, value);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename TImpl, typename TComparand>
|
||||||
|
inline bool operator==(const JsonVariantBase<TImpl> &variant,
|
||||||
|
TComparand comparand) {
|
||||||
|
typedef JsonVariantBase<TImpl> TVariant;
|
||||||
|
return JsonVariantComparer<TVariant, TComparand>::equals(variant, comparand);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename TImpl, typename TComparand>
|
||||||
|
inline bool operator==(TComparand comparand,
|
||||||
|
const JsonVariantBase<TImpl> &variant) {
|
||||||
|
typedef JsonVariantBase<TImpl> TVariant;
|
||||||
|
return JsonVariantComparer<TVariant, TComparand>::equals(variant, comparand);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename TImpl, typename TComparand>
|
||||||
|
inline bool operator!=(const JsonVariantBase<TImpl> &variant,
|
||||||
|
TComparand comparand) {
|
||||||
|
typedef JsonVariantBase<TImpl> TVariant;
|
||||||
|
return !JsonVariantComparer<TVariant, TComparand>::equals(variant, comparand);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename TImpl, typename TComparand>
|
||||||
|
inline bool operator!=(TComparand comparand,
|
||||||
|
const JsonVariantBase<TImpl> &variant) {
|
||||||
|
typedef JsonVariantBase<TImpl> TVariant;
|
||||||
|
return !JsonVariantComparer<TVariant, TComparand>::equals(variant, comparand);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename TImpl, typename TComparand>
|
||||||
|
inline bool operator<=(const JsonVariantBase<TImpl> &left, TComparand right) {
|
||||||
|
return left.template as<TComparand>() <= right;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename TImpl, typename TComparand>
|
||||||
|
inline bool operator<=(TComparand comparand,
|
||||||
|
const JsonVariantBase<TImpl> &variant) {
|
||||||
|
return comparand <= variant.template as<TComparand>();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename TImpl, typename TComparand>
|
||||||
|
inline bool operator>=(const JsonVariantBase<TImpl> &variant,
|
||||||
|
TComparand comparand) {
|
||||||
|
return variant.template as<TComparand>() >= comparand;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename TImpl, typename TComparand>
|
||||||
|
inline bool operator>=(TComparand comparand,
|
||||||
|
const JsonVariantBase<TImpl> &variant) {
|
||||||
|
return comparand >= variant.template as<TComparand>();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename TImpl, typename TComparand>
|
||||||
|
inline bool operator<(const JsonVariantBase<TImpl> &varian,
|
||||||
|
TComparand comparand) {
|
||||||
|
return varian.template as<TComparand>() < comparand;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename TImpl, typename TComparand>
|
||||||
|
inline bool operator<(TComparand comparand,
|
||||||
|
const JsonVariantBase<TImpl> &variant) {
|
||||||
|
return comparand < variant.template as<TComparand>();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename TImpl, typename TComparand>
|
||||||
|
inline bool operator>(const JsonVariantBase<TImpl> &variant,
|
||||||
|
TComparand comparand) {
|
||||||
|
return variant.template as<TComparand>() > comparand;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename TImpl, typename TComparand>
|
||||||
|
inline bool operator>(TComparand comparand,
|
||||||
|
const JsonVariantBase<TImpl> &variant) {
|
||||||
|
return comparand > variant.template as<TComparand>();
|
||||||
|
}
|
||||||
|
}
|
@ -5,8 +5,8 @@
|
|||||||
// https://github.com/bblanchon/ArduinoJson
|
// https://github.com/bblanchon/ArduinoJson
|
||||||
// If you like this project, please add a star!
|
// If you like this project, please add a star!
|
||||||
|
|
||||||
#include <gtest/gtest.h>
|
|
||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
class JsonVariant_Comparison_Tests : public ::testing::Test {
|
class JsonVariant_Comparison_Tests : public ::testing::Test {
|
||||||
protected:
|
protected:
|
||||||
@ -92,3 +92,37 @@ TEST_F(JsonVariant_Comparison_Tests, ULong) {
|
|||||||
TEST_F(JsonVariant_Comparison_Tests, UShort) {
|
TEST_F(JsonVariant_Comparison_Tests, UShort) {
|
||||||
testValue<unsigned short>(122, 123, 124);
|
testValue<unsigned short>(122, 123, 124);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_F(JsonVariant_Comparison_Tests, StringLiteral) {
|
||||||
|
DynamicJsonBuffer jsonBuffer;
|
||||||
|
JsonVariant variant = jsonBuffer.parse("\"hello\"");
|
||||||
|
|
||||||
|
ASSERT_TRUE(variant == "hello");
|
||||||
|
ASSERT_FALSE(variant != "hello");
|
||||||
|
|
||||||
|
ASSERT_TRUE(variant != "world");
|
||||||
|
ASSERT_FALSE(variant == "world");
|
||||||
|
|
||||||
|
ASSERT_TRUE("hello" == variant);
|
||||||
|
ASSERT_FALSE("hello" != variant);
|
||||||
|
|
||||||
|
ASSERT_TRUE("world" != variant);
|
||||||
|
ASSERT_FALSE("world" == variant);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(JsonVariant_Comparison_Tests, String) {
|
||||||
|
DynamicJsonBuffer jsonBuffer;
|
||||||
|
JsonVariant variant = jsonBuffer.parse("\"hello\"");
|
||||||
|
|
||||||
|
ASSERT_TRUE(variant == std::string("hello"));
|
||||||
|
ASSERT_FALSE(variant != std::string("hello"));
|
||||||
|
|
||||||
|
ASSERT_TRUE(variant != std::string("world"));
|
||||||
|
ASSERT_FALSE(variant == std::string("world"));
|
||||||
|
|
||||||
|
ASSERT_TRUE(std::string("hello") == variant);
|
||||||
|
ASSERT_FALSE(std::string("hello") != variant);
|
||||||
|
|
||||||
|
ASSERT_TRUE(std::string("world") != variant);
|
||||||
|
ASSERT_FALSE(std::string("world") == variant);
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user