JsonVariant::link()
Description
JsonVariant::link()
includes the specified value without making a copy.
In a way, it’s the opposite of JsonVariant::set()
, which makes a deep copy of the value.
JsonVariant::link()
allows you to embed a JsonDocument
inside another without increasing the memory consumption.
One could also describe this feature as “shallow copy.”
The embedded document appears as read-only from the point of view of the parent document.
In other words, if doc["link"]
points to another document, you cannot alter the value of doc["link"]["value"]
.
This feature was added in ArduinoJson 6.20.0
Signatures
void link(JsonVariantConst variant) const;
Arguments
variant
: the variant to link to.
Since JsonArray
, JsonDocument
, JsonObject
, and JsonVariant
convert to JsonVariantConst
, you can pass any of these classes to JsonVariant::link()
.
Example
void sendJsonRpcRequest(const char* method, JsonVariantConst params) {
StaticJsonDocument<64> doc;
doc["jsonrpc"] = "2.0";
doc["id"] = 1;
doc["method"] = method;
doc["params"].link(params);
serializeJson(doc, client);
}