Skip to content

Commit

Permalink
Fixed assignment of JsonDocument to JsonVariant (issue #1023)
Browse files Browse the repository at this point in the history
  • Loading branch information
bblanchon committed Jun 25, 2019
1 parent a0a4511 commit 2507ee2
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
ArduinoJson: change log
=======================

HEAD
----

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

v6.11.1 (2019-06-21)
-------

Expand Down
4 changes: 4 additions & 0 deletions src/ArduinoJson/Document/JsonDocument.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions src/ArduinoJson/Variant/VariantRef.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 16 additions & 0 deletions test/JsonVariant/set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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\"}");
}

0 comments on commit 2507ee2

Please sign in to comment.