mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-16 03:52:16 +02:00
Fixed JsonVariant::operator|(int)
to accept double (fixes #675)
This commit is contained in:
@ -1,6 +1,11 @@
|
|||||||
ArduinoJson: change log
|
ArduinoJson: change log
|
||||||
=======================
|
=======================
|
||||||
|
|
||||||
|
HEAD
|
||||||
|
----
|
||||||
|
|
||||||
|
* Fixed `JsonVariant::operator|(int)` that returned the default value if the variant contained a double (issue #675)
|
||||||
|
|
||||||
v5.13.0
|
v5.13.0
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
@ -6,6 +6,8 @@
|
|||||||
|
|
||||||
#include "Data/JsonVariantAs.hpp"
|
#include "Data/JsonVariantAs.hpp"
|
||||||
#include "Polyfills/attributes.hpp"
|
#include "Polyfills/attributes.hpp"
|
||||||
|
#include "TypeTraits/EnableIf.hpp"
|
||||||
|
#include "TypeTraits/IsIntegral.hpp"
|
||||||
|
|
||||||
namespace ArduinoJson {
|
namespace ArduinoJson {
|
||||||
namespace Internals {
|
namespace Internals {
|
||||||
@ -15,7 +17,8 @@ class JsonVariantOr {
|
|||||||
public:
|
public:
|
||||||
// Returns the default value if the JsonVariant is undefined of incompatible
|
// Returns the default value if the JsonVariant is undefined of incompatible
|
||||||
template <typename T>
|
template <typename T>
|
||||||
T operator|(const T &defaultValue) const {
|
typename EnableIf<!IsIntegral<T>::value, T>::type operator|(
|
||||||
|
const T &defaultValue) const {
|
||||||
if (impl()->template is<T>())
|
if (impl()->template is<T>())
|
||||||
return impl()->template as<T>();
|
return impl()->template as<T>();
|
||||||
else
|
else
|
||||||
@ -29,10 +32,21 @@ class JsonVariantOr {
|
|||||||
return value ? value : defaultValue;
|
return value ? value : defaultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns the default value if the JsonVariant is undefined of incompatible
|
||||||
|
// Special case for integers: we also accept double
|
||||||
|
template <typename Integer>
|
||||||
|
typename EnableIf<IsIntegral<Integer>::value, Integer>::type operator|(
|
||||||
|
const Integer &defaultValue) const {
|
||||||
|
if (impl()->template is<double>())
|
||||||
|
return impl()->template as<Integer>();
|
||||||
|
else
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const TImpl *impl() const {
|
const TImpl *impl() const {
|
||||||
return static_cast<const TImpl *>(this);
|
return static_cast<const TImpl *>(this);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
} // namespace Internals
|
||||||
}
|
} // namespace ArduinoJson
|
||||||
|
@ -51,6 +51,12 @@ TEST_CASE("JsonVariant::operator|()") {
|
|||||||
REQUIRE(result == 0);
|
REQUIRE(result == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SECTION("double | int") {
|
||||||
|
JsonVariant variant = 42.0;
|
||||||
|
int result = variant | 666;
|
||||||
|
REQUIRE(result == 42);
|
||||||
|
}
|
||||||
|
|
||||||
SECTION("bool | bool") {
|
SECTION("bool | bool") {
|
||||||
JsonVariant variant = false;
|
JsonVariant variant = false;
|
||||||
bool result = variant | true;
|
bool result = variant | true;
|
||||||
|
Reference in New Issue
Block a user