ESPAsyncWebServer
ESPAsyncWebServer copied to clipboard
What is the difference between declare the address with "jsonObj& doc" or "auto&& doc" (the first one didn't worked for me but the second did worked)
Hi all, how are you?.
I passed days trying to parse an JSON object from a web page until I found in the readme of the project and other sources how to parse it by using asyncjson.h
and arduinojson.h
, in the readme.me is an example who is written like follows
#include "AsyncJson.h"
#include "ArduinoJson.h"
AsyncCallbackJsonWebHandler* handler = new AsyncCallbackJsonWebHandler("/rest/endpoint", [](AsyncWebServerRequest *request, JsonVariant &json) {
JsonObject& jsonObj = json.as<JsonObject>();
// ...
});
server.addHandler(handler);
but that example didn't work for me because of the line
JsonObject& jsonObj = json.as<JsonObject>();
then I found in a web example a variation who was written like follows
auto&& jsonObj = json.as<JsonObject>();
I know that the that the auto
keyword is for inferring the type of variable from the right part of the expression but I don't understand why the &&
are written, to me it looks like if it was making a boolean operation but I don't know since I'm declaring what I think is an address variable whose content is the address of json
and, in the other hand, why the line
JsonObject& jsonObj = json.as<JsonObject>();
doesn't works at all?
I'm using platformio if it helps.
Thanks in advance for the help.
search engine: c++ && operator
-> https://stackoverflow.com/questions/4549151/c-double-address-operator -> https://en.cppreference.com/w/cpp/language/reference
in short words:
-
JsonObject& jsonObj = something();
– it will be a reference of thing returned by the function -
JsonObject&& jsonObj = something();
– is is "stealing" the value from the function -> it will be "the value itself", not a reference to it
Why the Lvalue reference does not work for you? I don't know what does json.as<JsonObject>()
do/return -> but my guess would be: your reference does not point to anything after all (because the value is destroyed at the end of the function).
Why the Rvalue reference works for you? I would guess, because you actually have the value then.
Question: have you tried just JsonObject jsonObj = json.as<JsonObject>();
? The value will probably be copied, but it should work, confirming my suspicions.
[STALE_SET] This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs. Thank you for your contributions.
search engine:
c++ && operator
-> https://stackoverflow.com/questions/4549151/c-double-address-operator -> https://en.cppreference.com/w/cpp/language/referencein short words:
JsonObject& jsonObj = something();
– it will be a reference of thing returned by the functionJsonObject&& jsonObj = something();
– is is "stealing" the value from the function -> it will be "the value itself", not a reference to itWhy the Lvalue reference does not work for you? I don't know what does
json.as<JsonObject>()
do/return -> but my guess would be: your reference does not point to anything after all (because the value is destroyed at the end of the function).Why the Rvalue reference works for you? I would guess, because you actually have the value then.
Question: have you tried just
JsonObject jsonObj = json.as<JsonObject>();
? The value will probably be copied, but it should work, confirming my suspicions.
Hi, sorry for the delay but thanks, your answer is the correct one