Fixed operator|(MemberProxy, JsonObject) (fixes #1415)

This commit is contained in:
Benoit Blanchon
2020-10-23 10:22:14 +02:00
parent ff66182dc6
commit 10ec0f21b0
7 changed files with 50 additions and 8 deletions

View File

@ -5,6 +5,7 @@ HEAD
----
* Fixed error `ambiguous overload for 'operator|'` (issue #1411)
* Fixed `operator|(MemberProxy, JsonObject)` (issue #1415)
v6.17.0 (2020-10-19)
-------

View File

@ -121,6 +121,17 @@ TEST_CASE("MemberProxy::operator|()") {
REQUIRE(sensor == std::string("gps"));
}
SECTION("Issue #1415") {
JsonObject object = doc.to<JsonObject>();
object["hello"] = "world";
StaticJsonDocument<0> emptyDoc;
JsonObject anotherObject = object["hello"] | emptyDoc.to<JsonObject>();
REQUIRE(anotherObject.isNull() == false);
REQUIRE(anotherObject.size() == 0);
}
}
TEST_CASE("MemberProxy::remove()") {

View File

@ -19,10 +19,13 @@ namespace ARDUINOJSON_NAMESPACE {
template <typename TArray>
class ElementProxy : public VariantOperators<ElementProxy<TArray> >,
public VariantShortcuts<ElementProxy<TArray> >,
public Visitable {
public Visitable,
public VariantTag {
typedef ElementProxy<TArray> this_type;
public:
typedef VariantRef variant_type;
FORCE_INLINE ElementProxy(TArray array, size_t index)
: _array(array), _index(index) {}

View File

@ -21,10 +21,13 @@ namespace ARDUINOJSON_NAMESPACE {
template <typename TObject, typename TStringRef>
class MemberProxy : public VariantOperators<MemberProxy<TObject, TStringRef> >,
public VariantShortcuts<MemberProxy<TObject, TStringRef> >,
public Visitable {
public Visitable,
public VariantTag {
typedef MemberProxy<TObject, TStringRef> this_type;
public:
typedef VariantRef variant_type;
FORCE_INLINE MemberProxy(TObject variant, TStringRef key)
: _object(variant), _key(key) {}

View File

@ -9,6 +9,7 @@
#include <ArduinoJson/Polyfills/attributes.hpp>
#include <ArduinoJson/Polyfills/type_traits.hpp>
#include <ArduinoJson/Variant/VariantAs.hpp>
#include <ArduinoJson/Variant/VariantTag.hpp>
namespace ARDUINOJSON_NAMESPACE {
@ -17,15 +18,24 @@ CompareResult compare(const T1 &lhs, const T2 &rhs); // VariantCompare.cpp
template <typename TVariant>
struct VariantOperators {
// Returns the default value if the VariantRef is undefined of incompatible
// Returns the default value if the VariantRef is undefined or incompatible
template <typename T>
friend typename enable_if<!IsVisitable<T>::value, T>::type operator|(
friend typename enable_if<!IsVariant<T>::value, T>::type operator|(
const TVariant &variant, T defaultValue) {
if (variant.template is<T>())
return variant.template as<T>();
else
return defaultValue;
}
// Returns the default value if the VariantRef is undefined or incompatible
template <typename T>
friend typename enable_if<IsVariant<T>::value, typename T::variant_type>::type
operator|(const TVariant &variant, T defaultValue) {
if (variant)
return variant;
else
return defaultValue;
}
// value == TVariant
template <typename T>

View File

@ -16,6 +16,7 @@
#include <ArduinoJson/Variant/VariantOperators.hpp>
#include <ArduinoJson/Variant/VariantRef.hpp>
#include <ArduinoJson/Variant/VariantShortcuts.hpp>
#include <ArduinoJson/Variant/VariantTag.hpp>
namespace ARDUINOJSON_NAMESPACE {
@ -23,12 +24,9 @@ namespace ARDUINOJSON_NAMESPACE {
class ArrayRef;
class ObjectRef;
template <typename, typename>
class MemberProxy;
// Contains the methods shared by VariantRef and VariantConstRef
template <typename TData>
class VariantRefBase {
class VariantRefBase : public VariantTag {
public:
// Tells wether the variant has the specified type.
// Returns true if the variant has type type T, false otherwise.

View File

@ -0,0 +1,16 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2020
// MIT License
#pragma once
#include <ArduinoJson/Namespace.hpp>
namespace ARDUINOJSON_NAMESPACE {
struct VariantTag {};
template <typename T>
struct IsVariant : is_base_of<VariantTag, T> {};
} // namespace ARDUINOJSON_NAMESPACE