Просмотр исходного кода

Fixed assignment of `JsonDocument` to `JsonVariant` (issue #1023)

Benoit Blanchon 6 лет назад
Родитель
Сommit
2507ee2e56

+ 5 - 0
CHANGELOG.md

@@ -1,6 +1,11 @@
 ArduinoJson: change log
 =======================
 
+HEAD
+----
+
+* Fixed assignment of `JsonDocument` to `JsonVariant` (issue #1023)
+
 v6.11.1 (2019-06-21)
 -------
 

+ 4 - 0
src/ArduinoJson/Document/JsonDocument.hpp

@@ -278,6 +278,10 @@ class JsonDocument : public Visitable {
     _data.remove(adaptString(key));
   }
 
+  FORCE_INLINE operator VariantConstRef() const {
+    return VariantConstRef(&_data);
+  }
+
  protected:
   JsonDocument(MemoryPool pool) : _pool(pool) {
     _data.setNull();

+ 1 - 0
src/ArduinoJson/Variant/VariantRef.hpp

@@ -222,6 +222,7 @@ class VariantRef : public VariantRefBase<VariantData>,
   // set(ArrayConstRef)
   // set(ObjectRef)
   // set(ObjecConstRef)
+  // set(const JsonDocument&)
   template <typename TVariant>
   typename enable_if<IsVisitable<TVariant>::value, bool>::type set(
       const TVariant &value) const;

+ 16 - 0
test/JsonVariant/set.cpp

@@ -108,3 +108,19 @@ TEST_CASE("JsonVariant with not enough memory") {
     REQUIRE(v.isNull());
   }
 }
+
+TEST_CASE("JsonVariant::set(DynamicJsonDocument)") {
+  DynamicJsonDocument doc1(1024);
+  doc1["hello"] = "world";
+
+  DynamicJsonDocument doc2(1024);
+  JsonVariant v = doc2.to<JsonVariant>();
+
+  // Should copy the doc
+  v.set(doc1);
+  doc1.clear();
+
+  std::string json;
+  serializeJson(doc2, json);
+  REQUIRE(json == "{\"hello\":\"world\"}");
+}