ESPAsyncWebServer icon indicating copy to clipboard operation
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)

Open VALERYRAMIREZ opened this issue 2 years ago • 3 comments

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.

VALERYRAMIREZ avatar Feb 04 '23 22:02 VALERYRAMIREZ

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.

MacDada avatar Mar 12 '23 00:03 MacDada

[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.

stale[bot] avatar May 21 '23 21:05 stale[bot]

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.

Hi, sorry for the delay but thanks, your answer is the correct one

VALERYRAMIREZ avatar Jul 28 '23 12:07 VALERYRAMIREZ