8000 Fixed assignment of `JsonDocument` to `JsonVariant` (issue #1023) · joglosemarduino/ArduinoJson@2507ee2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2507ee2

Browse files
committed
Fixed assignment of JsonDocument to JsonVariant (issue bblanchon#1023)
1 parent a0a4511 commit 2507ee2

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
ArduinoJson: change log
22
=======================
33

4+
HEAD
5+
----
6+
7+
* Fixed assignment of `JsonDocument` to `JsonVariant` (issue #1023)
8+
49
v6.11.1 (2019-06-21)
510
-------
611

src/ArduinoJson/Document/JsonDocument.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,10 @@ class JsonDocument : public Visitable {
278278
_data.remove(adaptString(key));
279279
}
280280

281+
FORCE_INLINE operator VariantConstRef() const {
282+
return VariantConstRef(&_data);
283+
}
284+
281285
protected:
282286
JsonDocument(MemoryPool pool) : _pool(pool) {
283287
_data.setNull();

src/ArduinoJson/Variant/VariantRef.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ class VariantRef : public VariantRefBase<VariantData>,
222222
// set(ArrayConstRef)
223223
// set(ObjectRef)
224224
// set(ObjecConstRef)
225+
// set(const JsonDocument&)
225226
template <typename TVariant>
226227
typename enable_if<IsVisitable<TVariant>::value, bool>::type set(
227228
const TVariant &value) const;

test/JsonVariant/set.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,19 @@ TEST_CASE("JsonVariant with not enough memory") {
108108
REQUIRE(v.isNull());
109109
}
110110
}
111+
112+
TEST_CASE("JsonVariant::set(DynamicJsonDocument)") {
113+
DynamicJsonDocument doc1(1024);
114+
doc1["hello"] = "world";
115+
116+
DynamicJsonDocument doc2(1024);
117+
JsonVariant v = doc2.to<JsonVariant>();
118+
119+
// Should copy the doc
120+
v.set(doc1);
121+
doc1.clear();
122+
123+
std::string json;
124+
serializeJson(doc2, json);
125+
REQUIRE(json == "{\"hello\":\"world\"}");
126+
}

0 commit comments

Comments
 (0)
0