user48730: I'm new to ArduinoJSON - so perhaps it is a newbie's question.... but I wish to pass a StaticJsonDocument into a function as a parameter ( later on it should be implemented in a library ).
exmaple below shows test_1 what I wish to obtain, but by duplicating StaticJsonDocument, which I don't want to do.
How can test_1 should be written (as I tried in test_2)?
#include <ArduinoJson.h>

StaticJsonDocument<200> doc;

void createJSON() {
        doc["sensor"] = "gps";
        doc["time"] = 1351824120;

        JsonArray data = doc.createNestedArray("data");
        data.add(48.756080);
        data.add(2.302038);

        // Generate the minified JSON and send it to the Serial port.
        serializeJson(doc, Serial);
        Serial.println("JSON is created:");

        // Generate the prettified JSON and send it to the Serial port.
        serializeJsonPretty(doc, Serial);

}


bool test_1(StaticJsonDocument<100> _doc){
        serializeJson(_doc, Serial);
        return true;
}

bool test2(const JsonObject& _doc){
        Serial.println("HI");
        serializeJson(_doc, Serial);
}


void setup() {
        Serial.begin(9600);

        createJSON();
        test_1(doc);


}

void loop() {
        // not used in this example
}


user37622: The StaticJsonDocument is a template class. The template value in <> is here only the size of the internal buffer, but every size used generates a different class. (memory usage!)
To have the parameter of the function take an StaticJsonDocument version instance, it must be the same version or a common base class. In this case the base class is JsonDocument.
If you don't use the reference symbol & the parameter is copied. In this case StaticJsonDocument<200> _doc or JsonDocument _doc would create a copy and that copy would be modified in the function. The object used as parameter value would be unchanged - empty.
So use
void test2(const JsonDocument& _doc) {
  Serial.println("HI");
  serializeJson(_doc, Serial);
}


48730: <p>I'm new to ArduinoJSON - so perhaps it is a newbie's question.... but I wish to pass a <code>StaticJsonDocument</code> into a function as a parameter ( later on it should be implemented in a library ).</p>

<p>exmaple below shows <code>test_1</code> what I wish to obtain, but by duplicating <code>StaticJsonDocument</code>, which I don't want to do.</p>

<p>How can <code>test_1</code> should be written (as I tried in <code>test_2</code>)?</p>

<pre><code>#include &lt;ArduinoJson.h&gt;

StaticJsonDocument&lt;200&gt; doc;

void createJSON() {
        doc["sensor"] = "gps";
        doc["time"] = 1351824120;

        JsonArray data = doc.createNestedArray("data");
        data.add(48.756080);
        data.add(2.302038);

        // Generate the minified JSON and send it to the Serial port.
        serializeJson(doc, Serial);
        Serial.println("JSON is created:");

        // Generate the prettified JSON and send it to the Serial port.
        serializeJsonPretty(doc, Serial);

}


bool test_1(StaticJsonDocument&lt;100&gt; _doc){
        serializeJson(_doc, Serial);
        return true;
}

bool test2(const JsonObject&amp; _doc){
        Serial.println("HI");
        serializeJson(_doc, Serial);
}


void setup() {
        Serial.begin(9600);

        createJSON();
        test_1(doc);


}

void loop() {
        // not used in this example
}
</code></pre>
 37622: <p>The <code>StaticJsonDocument</code> is a template class. The template value in &lt;> is here only the size of the internal buffer, but every size used generates a different class. (memory usage!)</p>

<p>To have the parameter of the function take an <code>StaticJsonDocument</code> version instance, it must be the same version or a common base class. In this case the base class is <code>JsonDocument</code>.</p>

<p>If you don't use the reference symbol <code>&amp;</code> the parameter is copied. In this case <code>StaticJsonDocument&lt;200&gt; _doc</code> or <code>JsonDocument _doc</code> would create a copy and that copy would be modified in the function. The object used as parameter value would be unchanged - empty.</p>

<p>So use</p>

<pre><code>void test2(const JsonDocument&amp; _doc) {
  Serial.println("HI");
  serializeJson(_doc, Serial);
}
</code></pre>
