user29992: I'd like my ESP8266 to provide some JSON endpoints. I want to use ArduinoJson for serialisation. While this works in general if I serialize into a String and return this String afterwards, I was wondering if there is really no way to do this without wasting so much memory. Shouldn't it be possible to do something like this? Any hints?
ESP8266WebServer server;
StaticJsonDocument<768> response;
response["night"] = _playback.isNight();
response["shuffle"] = _playback.isShuffle();
server.send_P(200, "application/json", NULL);
serializeJson(response, server.client());

(this gives the following error:)
no instance of overloaded function "serializeJson" matches the argument list -- argument types are: (ArduinoJson6141_0000010::StaticJsonDocument<768U>, WiFiServer::ClientType)


user3430: serializeJson() accepts a Print&, i.e, a reference to a class that implements the Print interface.
WiFiClient does implement the Print interface, but serializeJson() cannot make a reference to it because ESP8266WebServer::client() returns a temporary. Indeed, C++ forbids references to temporary variables, only const reference to temporaries are allowed.
To fix this compilation error, you must extract a variable:
WiFiClient client = server.client();
serializeJson(doc, client);

The way I see it, ESP8266WebServer::client() should return a WiFiClient& instead of a WiFiClient, just as HTTPClient::getStream() does. I opened issue #7075 about that.
Anyway, even it this fixes the compilation, I'm not sure it will work as you intend because ESP8266WebServer may not allow writing directly to the WiFiClient.

29992: <p>I'd like my ESP8266 to provide some JSON endpoints. I want to use ArduinoJson for serialisation. While this works in general if I serialize into a String and return this String afterwards, I was wondering if there is really no way to do this without wasting so much memory. Shouldn't it be possible to do something like this? Any hints?</p>

<pre><code>ESP8266WebServer server;
StaticJsonDocument&lt;768&gt; response;
response["night"] = _playback.isNight();
response["shuffle"] = _playback.isShuffle();
server.send_P(200, "application/json", NULL);
serializeJson(response, server.client());
</code></pre>

<p>(this gives the following error:)</p>

<pre><code>no instance of overloaded function "serializeJson" matches the argument list -- argument types are: (ArduinoJson6141_0000010::StaticJsonDocument&lt;768U&gt;, WiFiServer::ClientType)
</code></pre>
 3430: <p><a href="https://arduinojson.org/v6/api/json/serializejson/" rel="nofollow noreferrer"><code>serializeJson()</code></a> accepts a <code>Print&amp;</code>, i.e, a reference to a class that implements the <code>Print</code> interface.</p>

<p><code>WiFiClient</code> does implement the <code>Print</code> interface, but <a href="https://arduinojson.org/v6/api/json/serializejson/" rel="nofollow noreferrer"><code>serializeJson()</code></a> cannot make a reference to it because <code>ESP8266WebServer::client()</code> returns a temporary. Indeed, C++ forbids references to temporary variables, only <code>const</code> reference to temporaries are allowed.</p>

<p>To fix this compilation error, you must extract a variable:</p>

<pre class="lang-cpp prettyprint-override"><code>WiFiClient client = server.client();
serializeJson(doc, client);
</code></pre>

<p>The way I see it, <code>ESP8266WebServer::client()</code> should return a <code>WiFiClient&amp;</code> instead of a <code>WiFiClient</code>, just as <code>HTTPClient::getStream()</code> does. I opened <a href="https://github.com/esp8266/Arduino/issues/7075" rel="nofollow noreferrer">issue #7075</a> about that.</p>

<p>Anyway, even it this fixes the compilation, I'm not sure it will work as you intend because <code>ESP8266WebServer</code> may not allow writing directly to the <code>WiFiClient</code>.</p>
