me-no-dev / ESPAsyncWebServer

Async Web Server for ESP8266 and ESP32
3.7k stars 1.21k forks source link

code sample from "ArduinoJson Basic Response" does not compile #1353

Open shooshx opened 11 months ago

shooshx commented 11 months ago

This page: https://me-no-dev.github.io/ESPAsyncWebServer/ Has this code:

AsyncResponseStream *response = request->beginResponseStream("application/json");
DynamicJsonBuffer jsonBuffer;
JsonObject &root = jsonBuffer.createObject();
root["heap"] = ESP.getFreeHeap();
root["ssid"] = WiFi.SSID();
root.printTo(*response);
request->send(response);

which does not build. with Arduino IDE 1.8.19 Error message:

error: cannot bind non-const lvalue reference of type 'ArduinoJson::V6213PB2::JsonObject&' to an rvalue of type 'ArduinoJson::V6213PB2::JsonObject'
         JsonObject& root = response->getRoot();

This makes sense because getRoot() returns a JsonVariant which then needs to be temporarily converted to a JsonObject, but you can't bind a reference to a temporary object